setMemberState

View as Markdown

setMemberState

  • setMemberState(params): Promise<void>

Sets a state object for a member, for the specified channels. The previous state object will be completely replaced.

Parameters

params
objectRequired

Object containing the parameters of the method.

channels
string | string[]Required

Channels for which to set the state.

memberId
stringRequired

Id of the member to affect.

state
Record<any, any>Required

The state to set. There are no requirements on the content of the state.

Returns

Promise<void>

Example

In this example, we set the state of the member User1 in the channel channel1 to { online: true, typing: false }. We then fetch the state of the member and print it to the console.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const chatClient = client.chat;
6
7let usesId = 'User1'; // Assumes a member with this id already exists and has joined the channel.
8
9// set the member state
10await chatClient.setMemberState({
11 memberId: usesId,
12 channels: "channel1", // or a list of channels like: ["channel1", "channel2"]
13 state: {
14 online: true,
15 typing: false,
16 }
17});
18
19// get the member state
20const s = await chatClient.getMemberState({
21 channels: "channel1", // or a list of channels like: ["channel1", "channel2"]
22 memberId: usesId,
23});
24
25// print the state
26console.log(`The state of ${usesId} is: ${JSON.stringify(s.channels.channel1.state, null, 2)}`);