Monday, January 15, 2018

Messaging Pattern - With RxJS

While doing WPF development, one of the go to patterns is the messenger or pub/sub. The power of this pattern is individual components in a system can remain decoupled, yet communicate with each other. Reactive extensions for JavaScript make it easy to create a messenger for a modern client side application framework such as Angular.

The code flow is simple to describe. Two components use a common messenger service. In one component, a subscriber is registered against the service to listen for messages. In the other component the service is used to send a message. Both components rely on the service, but do not have any knowledge of each other.

Here is an example that can be adapted to your needs.

There are a few things to be aware of. If you do not use messaging class, you may fall into the trap of trying to message data received on an AJAX call . Your code will fail to pass the instance of  check, as you haven't actually created an object instance. This is one reason to follow the pattern of using a separate class for each type of message, instantiating the message and attaching the data.

For some applications it may work better to break the types of messages into categories. Each category can then have a method for sending and a Subject object to subscribe on. This reduces the amount of messaging noise on each channel.

No comments:

Post a Comment