UNPKG

@azure/communication-signaling

Version:

Azure Communication Signaling Client

95 lines (61 loc) 3.14 kB
# Azure Communication Signaling client library for JavaScript The Azure Communication Signaling client library lets developers add real-time notifications for receiving new/update/delete chat message events, typing indicator event, read receipt event, and chat thread events. Read more about Azure Communication Services [here](https://docs.microsoft.com/azure/communication-services/overview) ## Getting started ## Prerequisites - An [Azure subscription][azure_sub]. - An existing Communication Services resource. If you need to create the resource, you can use the [Azure Portal][azure_portal], the [Azure PowerShell][azure_powershell], or the [Azure CLI][azure_cli]. - [Node.js](https://nodejs.org) ### Installing ```bash npm install @azure/communication-signaling ``` ### Browser support #### JavaScript Bundle This client library is only supported in the browser. To use this client library, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling). ## Key concepts ### SignalingClient `SignalingClient` is the primary interface for developers using this client library. It provides methods to enable/disable real-time notifications and register listeners for different events. ## Examples ### Initialize SignalingClient Use user access token and Azure logger to initialize signaling client. ```JavaScript import { CommunicationSignalingClient } from '@azure/communication-signaling'; import { AzureCommunicationTokenCredential } from "@azure/communication-common"; import { createClientLogger } from "@azure/logger"; let userAccessToken = '<USER_ACCESS_TOKEN>'; let tokenCredential = new AzureCommunicationTokenCredential(userAccessToken); let logger = createClientLogger("communication-signaling"); let signalingClient = new CommunicationSignalingClient(tokenCredential, logger); ``` ### Enable real-time notifications ```JavaScript signalingClient.start(); ``` ### Disable real-time notifications ```JavaScript signalingClient.stop(); ``` ### Register listener to events With real-time signaling, you can subscribe to listen for new incoming events and call your own business logic accordingly. ```JavaScript // register listener to new incoming message event signalingClient.on("chatMessageReceived", (payload) => { console.log("Notification chatMessageReceived!"); // your code here }); // register listener to typing indicator event signalingClient.on("typingIndicatorReceived", (payload) => { console.log("Notification typingIndicatorReceived!"); // your code here }); // register listener to thread deleted event signalingClient.on("chatThreadDeleted", (payload) => { console.log("Notification chatThreadDeleted!"); // your code here }); ``` [azure_cli]: https://docs.microsoft.com/cli/azure [azure_sub]: https://azure.microsoft.com/free/ [azure_portal]: https://portal.azure.com [azure_powershell]: https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice