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 each other as well as multiple components 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 client side 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 authentication tasks in all the different languages on each platform or point of call is a time consuming and a complicated/multi computer language skilled task.

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

The router (node.js)

The dealer (node.js)

Message Manager:

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

Usage:

The dealer (php)

Message Manager:

Usage: