@steelbreeze/broker
Version:
Lightweight publish and subscribe using Server-Sent Events for node and express
45 lines (44 loc) • 2.02 kB
TypeScript
/** Specifies the location of a message broker server. */
export interface HTTPConfig {
/** The host that the message brokers express application is running on. */
host: string;
/** The port of the host that the message brokers express application is running on. */
port: number;
/** The base URL of the message broker. */
path: string;
}
export interface ErrorHandler {
(err: Error): void;
}
/** A message provided to a subscriber callback */
export interface ClientMessage {
/** The topic name that the message was published on. */
topicName: string;
/** The message broker server allocated id for the message; monotonically increments per topic. */
id: number;
/** The data published by the publisher. */
data: string;
}
export interface ClientCallback {
(message: ClientMessage): void;
}
/**
* Returns the client API to the message broker, providing publish and subscribe operations.
* @param config Configuration specifying the server, port and base URL path of the message broker server.
* @returns Returns a client providing publish and subscribe operations.
*/
export declare function client(config: HTTPConfig): {
/**
* Publishes a message to a message broker for other client to receive based on their subscriptions.
* @param topicName The topic to publish on. This may be one or more URL segments.
* @param data The data to publish on the topic
* @param onError Optional error handler callback
*/
publish: (topicName: string, data: string, onError?: ErrorHandler | undefined) => void;
/**
* Registers a subscription to messages on the given topic. The callback is called each time a message to published on the same topic.
* @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; passing a message object.
*/
subscribe: (topicName: string, callback: ClientCallback | undefined) => void;
};