UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

105 lines 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Realtime = void 0; const isomorphic_cometd_1 = require("isomorphic-cometd"); const MetaChannel_js_1 = require("./MetaChannel.js"); class Realtime { /** * only initializing cometd if it is actually needed * trying to save some memory here */ get cometd() { if (!this._cometd) { this._cometd = new isomorphic_cometd_1.CometD({}); // does not actually seem to be needed/supported? Just keeping to satisfy unit test snapshots this._cometd.websocketEnabled = true; this._cometd.addListener(MetaChannel_js_1.MetaChannel.HANDSHAKE, this.handshakeCallback || this.metaHandshake); } return this._cometd; } /** * Allows to set up a realtime (websocket or long-polling) connection to the platform. * @param client The fetch client instance to use * @param url The URL to connect to * @param handshakeCallback A function which is called on succeeded or failed handshake */ constructor(client, url = '/notification/realtime', handshakeCallback) { this.client = client; this.url = url; this.handshakeCallback = handshakeCallback; this.metaHandshake = msg => { if (!msg.successful) { throw new Error('Handshake failed'); } }; } /** * Subscribes to a realtime channel to listen for data. * @param channel The channel to connect to * @param callback A function to call when data is received */ subscribe(channel, callback) { this.checkConnection(); return this.cometd.subscribe(channel, callback); } /** * Cancels the listening to a channel. * @param subscription The subscription object returned by subscribe() */ unsubscribe(subscription) { return this.cometd.unsubscribe(subscription); } /** * Allows to listen to Cometd handshake to find out if e.g. the connection was reestablished. */ addHandshakeListener(callback) { return this.cometd.addListener(MetaChannel_js_1.MetaChannel.HANDSHAKE, callback); } /** * Allows to listen to Cometd connection to e.g. detect when connection was lost. */ addConnectListener(callback) { return this.cometd.addListener(MetaChannel_js_1.MetaChannel.CONNECT, callback); } /** * Removes the subscription obtained with a call to `addHandshakeListener` or `addConnectListener`. */ removeListener(subscriptionHandle) { return this.cometd.removeListener(subscriptionHandle); } /** * Resubscribes as necessary in case of a re-handshake. */ resubscribe(subscriptionHandle) { return this.cometd.resubscribe(subscriptionHandle); } /** * Disconnects the current connection. */ disconnect(disconnectCallback = undefined) { this.cometd.disconnect(disconnectCallback); } /** * Returns true if the CometD instance is disconnected or disconnecting. */ isDisconnected() { return this.cometd.isDisconnected(); } checkConnection() { const { cometd, client, url } = this; if (cometd.isDisconnected()) { const { headers } = client.getFetchOptions(); const config = { url: client.getUrl(url), requestHeaders: headers }; cometd.configure(config); this.handshake(client.getCometdHandshake()); } } handshake(config) { this.cometd.handshake(config, undefined); } } exports.Realtime = Realtime; //# sourceMappingURL=Realtime.js.map