pub-sub-js is tiny library with zero dependencies that implements distributed publish/subscribe messaging system.
$ npm install pub-sub-js
Why not use zeromq ? Because of this bug.
Usage
Publish messages to channels:
var Pub = Publisher; var publisher = ; publisher; ;
Subscribe to channels and receive the messages:
var Sub = Subscriber; var subscriber = ; subscriber;subscriber;subscriber; subscriber;
Output:
channel_one message_one
channel_two { message: 'two' }
Message three will not be received because there is no subscription to the channel_three. There will be no network traffic on publisher.send('channel_three', ...) call.
API
Class: Subscriber
.subscribe(channel)
Subscribes to the channel.
.unsubscribe(channel)
Unsubscribes from the channel.
.connect(port, [host])
Opens connection to the Publisher. See net.Socket.connect().
.disconnect(port, [host])
Closes the connection.
.listen(port, [host])
Begin accepting connections on the specified port and hostname. See net.Server.listen().
.close([callback])
Stops from accepting new connections. See net.Server.close().
Event: 'message' (channel, data)
Emitted when the message is received.
Event: 'log' (args)
Can be used for debugging.
Class: Publisher
.publish(channel, message)
Publish message to the channel.
.connect(port, [host])
Opens connection to the Subscriber. See net.Socket.connect().
.disconnect(port, [host])
Closes the connection.
.listen(port, [host])
Begin accepting connections on the specified port and hostname. See net.Server.listen().
.close([callback])
Stops from accepting new connections. See net.Server.close().
Event: 'log' (args)
Can be used for debugging.
Tests
$ npm test
License
MIT