@steelbreeze/broker
Advanced tools
Comparing version 1.0.0-beta.12 to 1.0.0-beta.13
@@ -10,4 +10,7 @@ /** Specifies the location of a message broker server. */ | ||
} | ||
export interface ErrorHandler { | ||
(err: Error): void; | ||
} | ||
/** A message provided to a subscriber callback */ | ||
export interface Message { | ||
export interface ClientMessage { | ||
/** The topic name that the message was published on. */ | ||
@@ -20,2 +23,5 @@ topicName: string; | ||
} | ||
export interface ClientCallback { | ||
(message: ClientMessage): void; | ||
} | ||
/** | ||
@@ -27,4 +33,4 @@ * Creates a client to the message broker, providing publish and subscribe operations. | ||
export declare function client(config: HTTPConfig): { | ||
publish: (topicName: string, data: string, onError?: ((err: Error) => void) | undefined) => void; | ||
subscribe: (topicName: string, callback: (message: Message) => void) => void; | ||
publish: (topicName: string, data: string, onError?: ErrorHandler | undefined) => void; | ||
subscribe: (topicName: string, callback: ClientCallback | undefined) => void; | ||
}; |
@@ -27,2 +27,3 @@ "use strict"; | ||
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) } }); | ||
// trap and propigate error messages as required | ||
post.on('error', function (err) { | ||
@@ -33,2 +34,3 @@ if (onError) { | ||
}); | ||
// send message to the server | ||
post.write(data); | ||
@@ -45,5 +47,8 @@ post.end(); | ||
var lastEventId = -1; | ||
// process messages from the server | ||
eventSource.onmessage = function (event) { | ||
// perform a duplicate message check | ||
if (event.lastEventId !== lastEventId) { | ||
lastEventId = event.lastEventId; | ||
// propigate message to the client | ||
if (callback) { | ||
@@ -55,4 +60,5 @@ callback({ topicName: topicName, id: event.lastEventId, data: event.data }); | ||
} | ||
// return the client API to the caller | ||
return { publish: publish, subscribe: subscribe }; | ||
} | ||
exports.client = client; |
{ | ||
"name": "@steelbreeze/broker", | ||
"version": "1.0.0-beta.12", | ||
"version": "1.0.0-beta.13", | ||
"description": "Lightweight publish and subscribe using Server-Sent Events for node and express", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -16,4 +16,16 @@ import * as http from 'http'; | ||
export interface ErrorHandler { | ||
(err: Error): void; | ||
} | ||
// A message provide by the server using Server-Sent Events | ||
interface ServerMessage { | ||
type: string; | ||
data: string; | ||
lastEventId: number; | ||
origin: string; | ||
} | ||
/** A message provided to a subscriber callback */ | ||
export interface Message { | ||
export interface ClientMessage { | ||
/** The topic name that the message was published on. */ | ||
@@ -29,2 +41,6 @@ topicName: string; | ||
export interface ClientCallback { | ||
(message: ClientMessage): void; | ||
} | ||
/** | ||
@@ -42,5 +58,6 @@ * Creates a client to the message broker, providing publish and subscribe operations. | ||
*/ | ||
function publish(topicName: string, data: string, onError: ((err: Error) => void) | undefined = undefined): void { | ||
function publish(topicName: string, data: string, onError: ErrorHandler | undefined = undefined): void { | ||
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) } }); | ||
// trap and propigate error messages as required | ||
post.on('error', (err: Error) => { | ||
@@ -52,2 +69,3 @@ if (onError) { | ||
// send message to the server | ||
post.write(data); | ||
@@ -62,10 +80,13 @@ post.end(); | ||
*/ | ||
function subscribe(topicName: string, callback: (message: Message) => void): void { | ||
function subscribe(topicName: string, callback: ClientCallback | undefined ): void { | ||
var eventSource = new EventSource(`http://${config.host}:${config.port}${config.path}/${topicName}`); | ||
var lastEventId = -1; | ||
eventSource.onmessage = (event: { type: string; data: string; lastEventId: number; origin: string; }): void => { | ||
// process messages from the server | ||
eventSource.onmessage = (event: ServerMessage): void => { | ||
// perform a duplicate message check | ||
if (event.lastEventId !== lastEventId) { | ||
lastEventId = event.lastEventId; | ||
// propigate message to the client | ||
if (callback) { | ||
@@ -78,3 +99,4 @@ callback({ topicName: topicName, id: event.lastEventId, data: event.data }); | ||
// return the client API to the caller | ||
return { publish, subscribe }; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24924
412