getMessages

View as Markdown

getMessages

  • getMessages(params): Promise<{ cursor: PaginationCursor, messages: ChatMessageEntity[] }>

Returns the list of messages that were sent to the specified channel.

Parameters

params
objectRequired

Object containing the parameters of the method.

channel
stringRequired

Channel for which to retrieve the messages.

cursor
PaginationCursor

Cursor for pagination. See PaginationCursor.

Returns

Promise<{ cursor: PaginationCursor, messages: ChatMessageEntity[] }>

A promise that resolves to an object containing the list of messages that were sent to the specified channel.

Example

In this example, we fetch all the messages sent in the channel channel1 and print them to the console. We use the cursor to fetch the next set of messages until the cursor is null, indicating that there are no more messages to fetch.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const chatClient = client.chat;
6
7let pageNumber = 1;
8let cursor = null;
9
10while (true) {
11 const m = await chatClient.getMessages({
12 channel: "channel1",
13 cursor: cursor ? { after: cursor } : undefined
14 });
15
16 console.log(`Page ${pageNumber}: fetched ${m.messages.length} messages
17 ${JSON.stringify(m.messages, null, 2)}`);
18
19 // Check if 'after' is null, indicating no more messages to fetch
20 if (!m.cursor.after) {
21 console.log("No more messages");
22 break;
23 }
24 // Update the cursor to fetch next set of messages
25 cursor = m.cursor.after;
26 pageNumber++;
27}