1.1.2 • Published 6 years ago
cometd-node-promise-client v1.1.2
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'));
2) 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
});
3) 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
});
4) If you wish to unsubscribe from a topic:
cometd.unsubscribe(subscription).then(() => {
// This promise resolves when the client has succesfully unsubscribed
});
5) 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.