> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes. # PubSub.Client You can use the Client object to build a messaging system into the browser. Example usage: ```js import { PubSub } from "@signalwire/js"; const pubSubClient = new PubSub.Client({ token: "", // get this from the REST APIs }); await pubSubClient.subscribe(["mychannel1", "mychannel2"]); pubSubClient.on("message", (message) => { // prettier-ignore console.log("Received", message.content, "on", message.channel, "at", message.publishedAt); }); await pubSubClient.publish({ channel: "mychannel1", content: "hello world", }); ``` ## Constructor Creates a new PubSub client. ```js import { PubSub } from "@signalwire/js"; const pubSubClient = new PubSub.Client({ token: "" }); ``` ### Parameters **`token`** `string` — required SignalWire Chat token that can be obtained from the [REST APIs](/docs/apis/rest/chat-tokens/create-chat-token). --- ## Methods #### [disconnect](/docs/browser-sdk/v3/js/reference/pubsub/client/disconnect) Disconnect the client #### [getAllowedChannels](/docs/browser-sdk/v3/js/reference/pubsub/client/get-allowed-channels) Get channels allowed by the token #### [publish](/docs/browser-sdk/v3/js/reference/pubsub/client/publish) Publish a message to a channel #### [subscribe](/docs/browser-sdk/v3/js/reference/pubsub/client/subscribe) Subscribe to channels #### [unsubscribe](/docs/browser-sdk/v3/js/reference/pubsub/client/unsubscribe) Unsubscribe from channels #### [updateToken](/docs/browser-sdk/v3/js/reference/pubsub/client/update-token) Replace the client token #### [on](/docs/browser-sdk/v3/js/reference/pubsub/client/on) Subscribe to an event #### [once](/docs/browser-sdk/v3/js/reference/pubsub/client/once) Subscribe to an event once #### [off](/docs/browser-sdk/v3/js/reference/pubsub/client/off) Unsubscribe from an event #### [removeAllListeners](/docs/browser-sdk/v3/js/reference/pubsub/client/remove-all-listeners) Remove all event listeners ## Events #### [Events](/docs/browser-sdk/v3/js/reference/pubsub/client/events) Events emitted by the PubSub.Client class. ## Docs - [Events](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/events.md) - [disconnect](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/disconnect.md): disconnect method for the PubSub.Client class. - [getAllowedChannels](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/get-allowed-channels.md): getAllowedChannels method for the PubSub.Client class. - [off](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/off.md): off method for the PubSub.Client class. - [on](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/on.md): on method for the PubSub.Client class. - [once](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/once.md): once method for the PubSub.Client class. - [publish](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/publish.md): publish method for the PubSub.Client class. - [removeAllListeners](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/remove-all-listeners.md): removeAllListeners method for the PubSub.Client class. - [subscribe](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/subscribe.md): subscribe method for the PubSub.Client class. - [unsubscribe](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/unsubscribe.md): unsubscribe method for the PubSub.Client class. - [updateToken](https://signalwire.com/docs/browser-sdk/v3/js/reference/pubsub/client/update-token.md): updateToken method for the PubSub.Client class.