PubSub.Client
Constructors
• new Client(pubSubOptions
)
Creates a new PubSub client.
Parameters
Name | Type | Description |
---|---|---|
pubSubOptions | Object | - |
pubSubOptions.project | string | SignalWire project id, e.g. |
pubSubOptions.token | string | SignalWire API token |
Example
import { PubSub } from '@signalwire/realtime-api'
const pubSubClient = new PubSub.Client({
project: '<project-id>',
token: '<api-token>'
})
Methods
▸ off<T
>(event
, fn?
): EmitterContract
<PubSubClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof PubSubClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn? | (...args : ArgumentMap <PubSubClientApiEvents >[Extract <T , keyof PubSubClientApiEvents >]) => void |
Returns
EmitterContract
<PubSubClientApiEvents
>
▸ on<T
>(event
, fn
): EmitterContract
<PubSubClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof PubSubClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn | (...args : ArgumentMap <PubSubClientApiEvents >[Extract <T , keyof PubSubClientApiEvents >]) => void |
Returns
EmitterContract
<PubSubClientApiEvents
>
▸ once<T
>(event
, fn
): EmitterContract
<PubSubClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof PubSubClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn | (...args : ArgumentMap <PubSubClientApiEvents >[Extract <T , keyof PubSubClientApiEvents >]) => void |
Returns
EmitterContract
<PubSubClientApiEvents
>
▸ publish(params
): Promise
<void
>
Publish a message into the specified channel.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.channel | string | Channel in which to send the message. |
params.content | any | The message to send. This can be any JSON-serializable object. |
params.meta? | Record <any , any > | Metadata associated with the message. There are no requirements on the content of metadata. |
Returns
Promise
<void
>
Examples
Publishing a message as a string:
await pubSubClient.publish({
channel: 'my-channel',
content: 'Hello, world.'
})
Publishing a message as an object:
await pubSubClient.publish({
channel: 'my-channel',
content: {
field_one: 'value_one',
field_two: 'value_two',
}
})
▸ removeAllListeners<T
>(event?
): EmitterContract
<PubSubClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof PubSubClientApiEvents |
Parameters
Name | Type |
---|---|
event? | T |
Returns
EmitterContract
<PubSubClientApiEvents
>
▸ subscribe(channels
): Promise
<void
>
List of channels for which you want to receive messages.
Note that the subscribe
function is idempotent, and calling it again with a different set of channels will not unsubscribe you from the old ones. To unsubscribe, use unsubscribe.
Parameters
Name | Type | Description |
---|---|---|
channels | string | string [] | the channels to subscribe to, either in the form of a string (for one channel) or an array of strings. |
Returns
Promise
<void
>
Example
const pubSubClient = new PubSub.Client({
project: '<project-id>',
token: '<api-token>'
})
await pubSubClient.subscribe("my-channel")
await pubSubClient.subscribe(["chan-2", "chan-3"])
▸ unsubscribe(channels
): Promise
<void
>
List of channels from which you want to unsubscribe.
Parameters
Name | Type | Description |
---|---|---|
channels | string | string [] | the channels to unsubscribe from, either in the form of a string (for one channel) or an array of strings. |
Returns
Promise
<void
>
Example
await pubSubClient.unsubscribe("my-channel")
await pubSubClient.unsubscribe(["chan-2", "chan-3"])