Client
A PubSub client is used to listen for events on a channel and publish messages to a channel.
PubSub Client
Setting up a PubSub Client
To create a PubSub
client, you will first need to create a SignalWire Realtime-Client
.
After the SignalWire Client
is created, you can access the PubSub
client using the pubSub
namespace.
import { SignalWire} from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
const pubSubClient = client.pubSub;
Methods
listen
▸ listen({ event: Callback }
): Promise
<PubSub Events
>
Listen for events on the specified channel.
Parameters
Name | Type | Description |
---|---|---|
params | Object | Object containing the parameters of the method. |
params.channels | string[] | List of channels to listen to. |
params.EVENT | string | The event to listen to. List of events can be found here. Example event: onMessageReceived ) |
Returns
Promise
<PubSubEvents
>
A promise that resolves to a PubSubEvents
object
that you can use to view the current state or results of the event.
Example
import { SignalWire} from "@signalwire/realtime-api";
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
const pubSubClient = client.pubSub;
await pubSubClient.listen({
channels: ["my-channel"],
onMessageReceived: (message) => {
console.log(message);
}
});
In this example:
- Import the SignalWire module and initialize a new SignalWire client using your project ID and token.
- Access the PubSub client from the initialized SignalWire client.
- Subscribe to the
my-channel
channel and set up a listener for incoming messages. - Log received messages to the console.
publish
▸ 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:
import { SignalWire } from '@signalwire/realtime-api'
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
const pubSubClient = client.pubSub;
await pubSubClient.publish({
channel: "my-channel",
content: "Hello, world."
});
Publishing a message as an object:
import { SignalWire } from '@signalwire/realtime-api'
const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
const pubSubClient = client.pubSub;
await pubSubClient.publish({
channel: "my-channel",
content: {
field_one: "value_one",
field_two: "value_two"
}
});
Events
onMessageReceived
• client.pubSub.listen({ onMessageReceived: Callback }
)
Emitted when a message is received on a channel. Your event handler will be called with an instance of PubSubMessage
.
Parameters
Name | Type | Description |
---|---|---|
message | PubSubMessage | The message that has been received. |