In the previous part, I added the basic authentication and authorization to the API service. In this part, I am going to implement the pub/sub messaging service using Faye and integrate it with the API service.

If you are not familiar with publish-subscribe design pattern, you might want to do some reading before continue reading. I am gonna give a brief explanation about pub/sub design pattern. The idea behind this design pattern is to eliminate direct dependencies between services. In this design pattern, services communicate with each other through channels instead of direct calls. There can be one or multiple channels to which services will listen for incoming messages or send messages. When a service wants to notify another service about an event, it sends a message to the appropriate channel to which the targeted service is listening. The targeted service then receives the message and does something with it.

Faye implements the simplest form of pub/sub. There are many other pub/sub libraries in nodejs world such as Socket.io or SockJS. There are also paid services such as PubNub.

But I probably don’t want to allow everyone to push messages to my pub/sub server. Instead I want to make it read-only from the UI point of view, and only allow the API service to push messages. Faye’s extensions are designed for this purpose

This is enough if I don’t have the secret key set up on the other side. Since I have it, I need to add another extension to insert the secret key to every message pushed to pub/sub channels from the API service.