broker
Lightweight publish and subscribe using Server-Sent Events for node and express
If you like broker, please star it...
Install
npm i @steelbreeze/broker
Usage
Server
To create a message broker server within an express application:
const express = require('express');
const broker = require('@steelbreeze/broker');
var app = express();
var events = broker.server({lastMessage: true});
app.use('/events', events);
app.listen(1024, 'localhost');
You can create multiple message broker servers and bind them to different base URLs in the same express application.
Subscriber (node)
To create a subscription:
const broker = require('@steelbreeze/broker');
var client = broker.client({host:'localhost', port: 1024, path: '/events'});
client.subscribe('devices', (message) => {
console.log(`All devices: (${message.id}) ${message.data}`);
});
A single client can subscribe to multiple topics.
Publisher (node)
To create a subscription:
const broker = require('@steelbreeze/broker');
var client = broker.client({host:'localhost', port: 1024, path: '/events'});
var timer = setInterval( () => {
client.publish('devices', `Hello at ${new Date()}`, onError);
}, 1000);
function onError() {
clearInterval(timer);
}
Web clients
In addition to using the provided client, a browser's EventSource
may be used to subscribe and an HTTP POST can be used to publish.
Note: for some browsers this may require an EventSource polyfill.
License
MIT License
Copyright (c) 2018 David Mesquita-Morris