Singleton websocket publisher with Autobahn pubsub & Meta events

In the bigdata analytics architectures centered around Hadoop ecosystem, Zookeeper plays an important role of coordinating the resources and resource ownership. For example, in a distributed cluster with multiple nodes serving the same resource, you can implement fault-tolerance by selecting a dormant node as active resource-owner whenever the active node goes down.

How do you implement such failover mechanism for pubsub publishers?

There are many pubsub frameworks out there, such as MQTT, ZMQ etc. which work great with custom protocols, but fall short when you try to make them work with HTTP (browser-based). That is where Autobahn js shines. Utilizing the basic http/websocket protocol, it lets you create a pub-sub server as easily as writing a typical web-socket server.

But consider this question:

can you run a pubsub publisher entirely in browser (and not as hosted backend)?

It is in fact possible, thanks to Autobahn websockets pubsub mechanism. You can create two separate html pages, one for pubsub client and one for pubsub server, and launch the pubsub server html page. All client pages will get messages published by the server page. Only as long as the server page remains loaded in the browser, that is. If you close the browser window of the server html page, then you will loose your publishing server !!

How to make the publisher not die, when there are subscribers still around?

One technique that we show in the below code is: re-spawn the publishing server whenever an active instance goes down, thus effectively implementing the fail-over scenario.

The challenge in such approach is: detecting whenever the publisher is going down (dead). This is where the Metaevents of Autobahn comes into picture. It lets you track meta events about connections, such as session join, leave, rpc method registered, unregistered etc. To take advantage of these meta events, you should use some capable broker such as

For a browser-based subscriber to detect these events and spawn the publisher automatically whenever the active instance fails, it should implement a singleton pattern for the publisher resource (since it is supposed to be shared by all subscribed clients simultaneously.)

Below code demonstrates how to implement singleton pattern for the shared publisher by using the rpc call registration feature. By default, Autobahn rejects duplicate registrations for the same remote method uri. The code builds on this feature to start the publisher as a singleton instance.

To test this code, save the code as html page and launch multiple instances of it in different browser tags and view the console logs. You can see only one active instance is at any time treated as publisher, while all other instances are treated as subscribers. When you close the active publisher instance browser tab, the other instance automatically elect another instance among themselves as the publisher and continue as usual.