npm.io
1.1.2 • Published 7 years ago

cometd-node-promise-client

Licence
Apache-2.0
Version
1.1.2
Deps
3
Size
16 kB
Vulns
2
Weekly
0
Stars
1
DeprecatedThis package is deprecated

CometD client for Node.js that adds support for promises

Installation

Add the client to your project by running:
npm install cometd-node-promise-client

Usage

  1. Instantiate the CometD client

1.a) with the default console logger:

const cometd = new CometdClient();

OR

1.b) with the Winston logger:

Winston.loggers.add('COMETD', {
  console: { level: 'info', colorize: true, label: 'COMETD' }
});
const cometd = new CometdClient(Winston.loggers.get('COMETD'));
  1. Configure the CometD client
cometd.configure({
    url: cometdServerUrl,
    requestHeaders: { Authorization: 'Bearer ' + this.session.access_token }, // Optional parameter if you wan to pass additional headers
    appendMessageTypeToURL: false
});
  1. Handshake with the CometD server and subscribe to a topicName topic:
cometd.connect().then(() => {
    // Connection succeeded
    cometd.subscribe(topicName, onMessageReceived).then(subscription => {
        // This promise resolves when the client has succesfully subscribed
    });
});

The onMessageReceived callback function is executed each time a topicName message is received.

Alternatively, you can batch subscriptions (network optimization):

const cometdPromises = [
    cometd.subscribe(topic1, onMessage1Received),
    cometd.subscribe(topic2, onMessage2Received)
];
cometd.batch(cometdPromises).then(() => {
    // This promise resolves when the batch is processed
});
  1. If you wish to unsubscribe from a topic:
cometd.unsubscribe(subscription).then(() => {
    // This promise resolves when the client has succesfully unsubscribed
});
  1. When you no longer need your CometD client, disconnect from the server:
cometd.disconnect().then(() => {
    // This promise resolves when the client has succesfully disconnected
});

disconnect() unsubscribes from all active subscriptions.

Keywords