PubSub
Pusher system using the publisher/subscriber model; supports clustering
Design
- Peers: Your application (logic) servers clustered for performance
- Connected to Master via. TCP or Unix/Windows Socket (set via
remote
option) - Peers can communicate with each other via the
PubSub.Peer
API - Peers can communicate with master via the
PubSub.Master
API - Peers can communicate with clients via the
PubSub.Client
API
- Connected to Master via. TCP or Unix/Windows Socket (set via
- Clients: Clients which are communicating with your application servers
- Connected to Master via. WebSocket (Socket.IO)
- Clients can listen to information sent by the application servers
- Clients can respond back when an application sends a request
- Master: Contains the PubSub hub that connects your users (connected via WebSockets) to your application servers
- Master can communicate with application servers via the
PubSub.Slave
API
- Master can communicate with application servers via the
Install
Install with npm
npm install git://github.com/bluejamesbond/PubSub.js.git
Spin up this example (via. BranchOff)
npm install pm2 -g
pm2 install branch-off
// go to to localhost:5000 (build/build)
// add this repo and the "master" branch
Communicate Master ↔ Peer
Master
const app = ;const server = http;const pubsub = 'master-server-0' debug remote; pubsub; const Slave = pubsub; Slave; Slave;
Peer
// connect to masterconst master = 'master-server-0'; // master nameconst address = port: 3000 protocol: 'http' hostname: '0.0.0.0'; // master addressconst ps = 'my-name' address remote master; ps; const Master = ps; ps;
Communicate Peer ↔ Client
Peer
// ... connect to master const {Client} = ps; Client.accept('user-01'); // only accepted users can connect Client.on('connect-user-01', async () => { const res = await Client.emit('foo'); console.log(res); // 'bar'});
Client
socket = io(url, {query: 'id=user-01'}); socket.once('foo', respond => respond('bar');});
Communicate Peer ↔ Peer
Peer 1
// ... connect to master const {Peer} = ps; ps.on('connect', async () => { const res = await Peer.emit('add', [5, 4]); console.log(res); // 9});
Peer 2
// ... connect to master const {Peer} = ps; Peer.on('add', ([a, b]) => { respond(a + b);});