Creating a Router and Dealer messaging system using ZeroMQ, Node.js and PHP

-

Introduction

When building distributed systems, there is usually the need for more than just a simple one to one communication (client and server).

The router and dealer communication method allows many components to speak to eachother aswell as multiple componenents to speak to one other component e.g. router.

Node.js and ZeroMQ allows us to asynchronously collect and respond to dealers messages.

Example

Lets imagine that you have a social platform that requires authentication. Now this authentication doesn’t just occur at a single point of entry (login) but it also occurs during HTML5 websocket bi-directional communication on the clientside at a regular heart beat, and maybe at regular intervals on a mobile app.

Having to write a script to directly access the database and do authenication tasks in all the different lanaguages on each platform or point of call is a time consuming and a complicated/multi computer langauge skilled task.

Lukily, instead of the above ZeroMQ opens opportunites to create a central system (with a router) and multiple dealers (at each point of authentication) so the real authenication logic and database access only has to be written at one plaec in the appropriate languages.

The Router (Node.js)

The Dealer (Node.js)

Mesage Manager:

Tip: Notice ‘this.socket.once‘, this makes sure that the socket doesnt get bound each time its called. Its easy to just use this.socket.bind() but that will create a que of multiple javascript bindings.

Usage:

The Dealer (PHP)

Mesage Manager:

Usage:

Comments

Visitor stats.