Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@steelbreeze/broker

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@steelbreeze/broker - npm Package Compare versions

Comparing version 1.0.0-beta.6 to 1.0.0-beta.7

examples/publisher.js

6

lib/client.d.ts

@@ -12,4 +12,8 @@ /** Specifies the location of a message broker server. */

publish: (topicName: string, data: string, onError: ((err: Error) => void) | undefined) => void;
subscribe: (topicName: string, callback: (data: string) => void) => void;
subscribe: (topicName: string, callback: (message: {
topicName: string;
id: number;
data: string;
}) => void) => void;
};
export {};

11

lib/client.js

@@ -17,6 +17,11 @@ "use strict";

* @param data The data to publish on the topic
* @param onError Optional error handler callback
*/
function publish(topicName, data, onError) {
var post = http.request({ hostname: config.host, port: config.port, path: config.path + "/" + topicName, method: 'POST', headers: { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(data) } });
post.on('error', onError || (function () { }));
post.on('error', function (err) {
if (onError) {
onError(err);
}
});
post.write(data);

@@ -28,3 +33,3 @@ post.end();

* @param topicName The topic to subscribe to. This may be one or more URL segments.
* @param callback The function to call when data is publised on the topic.
* @param callback The function to call when data is publised on the topic; passing a message object containing the message id and data.
*/

@@ -38,3 +43,3 @@ function subscribe(topicName, callback) {

if (callback) {
callback(event.data);
callback({ topicName: topicName, id: event.lastEventId, data: event.data });
}

@@ -41,0 +46,0 @@ }

{
"name": "@steelbreeze/broker",
"version": "1.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "Lightweight publish and subscribe using Server-Sent Events for node and express",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -20,10 +20,13 @@ # broker

// create a message broker server and bind it to the /events base URL
app.use('/events', broker.server({lastMessage: true}));
// create a message broker that provides the last message on subscription
var events = broker.server({lastMessage: true});
// bind the message broker to the /events base URL
app.use('/events', events);
// start the express application
app.listen(1024, 'localhost');
```
You can create multiple message broker servers and bind them to different base URLs.
### Subscriber
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:

@@ -38,7 +41,7 @@ ```javascript

client.subscribe('devices', (message) => {
console.log(`devices: ${message}`);
console.log(`All devices: (${message.id}) ${message.data}`);
});
```
A single client can subscribe to multiple topics.
```
### Publisher
### Publisher (node)
To create a subscription:

@@ -52,6 +55,13 @@ ```javascript

// publish a message on the devices topic of the /events broker every second
setInterval( () => {
client.publish('devices', `Hello devices at ${new Date()}`);
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

@@ -58,0 +68,0 @@ MIT License

@@ -21,2 +21,3 @@ import * as http from 'http';

* @param data The data to publish on the topic
* @param onError Optional error handler callback
*/

@@ -26,3 +27,8 @@ function publish(topicName: string, data: string, onError: ((err: Error) => void) | undefined): void {

post.on('error', onError || (() => { }));
post.on('error', (err: Error) => {
if (onError) {
onError(err);
}
});
post.write(data);

@@ -35,5 +41,5 @@ post.end();

* @param topicName The topic to subscribe to. This may be one or more URL segments.
* @param callback The function to call when data is publised on the topic.
* @param callback The function to call when data is publised on the topic; passing a message object containing the message id and data.
*/
function subscribe(topicName: string, callback: (data: string) => void): void {
function subscribe(topicName: string, callback: (message: { topicName: string, id: number, data: string }) => void): void {
var eventSource = new EventSource(`http://${config.host}:${config.port}${config.path}/${topicName}`);

@@ -47,3 +53,3 @@ var lastEventId = -1;

if (callback) {
callback(event.data);
callback({ topicName: topicName, id: event.lastEventId, data: event.data });
}

@@ -50,0 +56,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc