
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@rc-ex/ws
Advanced tools
WebSocket Extension adds support for WebSocket protocol.
Please read this article: Create WebSocket subscriptions using RingCentral JavaScript SDKs.
yarn add @rc-ex/ws
import RingCentral from '@rc-ex/core';
import WebSocketExtension from '@rc-ex/ws';
const rc = new RingCentral(...);
const webSocketExtension = new WebSocketExtension(webSocketOptions);
await rc.installExtension(webSocketExtension);
You can setup subscriptions:
await webSocketExtension.subscribe(
['/restapi/v1.0/account/~/extension/~/message-store'],
event => {
...
}
);
You can also make Rest API calls over WebSocket if you specified webSocketOptions.restOverWebSocket
as true
:
const webSocketExtension = new WebSocketExtension({
restOverWebSocket: true,
});
await rc.installExtension(webSocketExtension);
const extInfo = await rc.restapi().account().extension().get();
console.log(extInfo.id);
Each WebSocketExtension
can only have 1 subscription. Trying to invoke subscribe
multiple times will override the previous subscriptions.
If you need to create multiple subscriptions, you need to create multiple WebSocketExtension
.
WebSocketExtension
constructor requires WebSocketOptions
as parameter:
type WebSocketOptions = {
restOverWebSocket?: boolean;
debugMode?: boolean;
autoRecover?: boolean;
};
restOverWebSocket
indicates whether to make Rest API call over WebSocket protocol.
Default value is false.
Please note that, not all Rest API calls can be done over WebSocket protocol. The following are not supported at the moment:
form-data/multipart
POST / PUTIf restOverWebSocket
is true and an Rest API call cannot be done over WebSocket, it will be done over HTTPS instead.
debugMode
indicates whether to enable debug mode.
Default value is false.
If enabled, WebSocket incoming message and outgoing message will be printed using console.debug
.
autoRecover
indicates whether to auto recover when WebSocket connection is lost.
Default value is true.
If disabled, you need to manually invoke await webSocketExtension.recover()
whenever WebSocket connection is lost.
webSocketExtension.ws;
gives you the WebSocket object. But if the network is unstable and autoRecover
is enabled, sometimes a new WebSocket connection will be created to replace the current one.
You can listen for autoRecoverSuccess
& autoRecoverFailed
events:
import {Events} from '@rc-ex/ws';
...
webSocketExtension.eventEmitter.on(Events.autoRecoverSuccess, ws => {
console.log(`New WebSocket connection: ${ws}`);
});
webSocketExtension.eventEmitter.on(Events.autoRecoverFailed, ws => {
console.log(`New WebSocket connection: ${ws}`);
});
By default auto recover is enabled. You can subscribe to auto recover events:
webSocketExtension.eventEmitter.on(Events.autoRecoverSuccess, (ws) => {
console.log(`auto recover success: ${ws}`);
});
webSocketExtension.eventEmitter.on(Events.autoRecoverFailed, (ws) => {
console.log(`auto recover failed: ${ws}`);
});
webSocketExtension.eventEmitter.on(Events.autoRecoverError, (error) => {
console.log(`auto recover error: ${error}`);
});
autoRecoverError
means cannot connect to WebSocket server at all. There will be more tries. So autoRecoverError
does NOT mean auto recover gives up trying.
autoRecoverFailed
means connection to WebSocket server has been restored, but existing subscriptions haven't. The SDK automatically created new subscriptions for you.
recoveryTimeout
and client side gives up recovery but creates brand new connection instead."recoveryState": "Failed"
autoRecoverSuccess
means connection to WebSocket server has been restored, and existing subscriptions have been restored too. And server side keeps your notification messages in a buffer and it will send you the messages soon.
recoveryBufferSize
setting on server side. If there are too many messages queued before session recover success, oldest messages will be discarded.In case of network outage and the WebSocket connection is lost, you can restore the session by:
await webSocketExtension.recover();
Command above will create a new WebSocket connection and make sure that subscriptions are recovered.
When you refresh page, you lost everything currently in the page's memory.
Below is the recommended way to recover. It may not be the best practice, but we have tested it and it does work.
Ref: https://github.com/tylerlong/rc-ws-refresh-page-demo/blob/main/src/index.ts
Before page refresh:
const webSocketExtension = new WebSocketExtension();
webSocketExtension.eventEmitter.on(Events.newWsc, async (wsc: Wsc) => {
await localforage.setItem(wscTokenKey, wsc.token);
});
await rc.installExtension(webSocketExtension);
...
const subscription = await webSocketExtension.subscribe(
['/restapi/v1.0/account/~/extension/~/message-store'],
event => callback(event)
);
await localforage.setItem(subscriptionKey, subscription.subscriptionInfo);
After page refresh:
const wscToken = (await localforage.getItem(wscTokenKey)) as string;
const webSocketExtension = new WebSocketExtension({wscToken});
...
const subscriptionInfo = (await localforage.getItem(
subscriptionKey
)) as SubscriptionInfo;
const subscription = await webSocketExtension.subscribe(
['/restapi/v1.0/account/~/extension/~/message-store'],
event => callback(event),
subscriptionInfo // restore old one. new one will be created instead if this parameter is undefined or null
);
FAQs
WebSocket extension for ringcentral-extensible project
We found that @rc-ex/ws demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.