Chat

View as Markdown

Access the Chat API Consumer. You can instantiate a Chat.Client to subscribe to Chat events.

For a full list of events that a Chat.Client can subscribe to, refer to Chat Events.

Example

The following example shows how to instantiate a Chat.Client and listen to the onMemberJoined event on the channel1 channel. When a member joins the channel, the bot will send a message to the channel with the number of members in the channel and the list of members.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const chatClient = client.chat;
6
7await chatClient.listen({
8 channels: ["channel1"],
9 onMemberJoined: async (member) => {
10
11 let members = await chatClient.getMembers({
12 channel: member.channel
13 });
14 let chatMessage = `Hello ${member.id}!
15 There are ${members.members.length} members in this channel.
16 The members are: ${members.members.map(m => m.id).join(", ")}`
17
18 await chatClient.publish({
19 channel: member.channel,
20 content: chatMessage
21 })
22 }
23});

Classes