Skip to main content

The Chat Namespace

The Chat namespace includes methods that allows you to send and receive chat messages.

The ConversationChatMessage object​

ConversationChatMessage objects represent the specific interactions that have taken place in the Chat:

  • id: A unique identifier for the conversation message.
  • conversation_id: A unique identifier of the parent conversation.
  • user_id: A unique identifier for the subscriber.
  • ts: The timestamp, in Unix Epoch, when the message was created.
  • details: Additional metadata associated with the conversation.
  • type: The type of the conversation message.
  • subtype: The subtype of the conversation message.

Here is an example of a ConversationChatMessage object:

{
"id": "3c114417-5cc0-4d03-a30f-c90659028d73",
"conversation_id": "9dbe9e94-c797-461e-b5a3-af6d095deaf4",
"user_id": "cecbe021-ff86-4ac6-bdf3-399ca477ad6f",
"ts": 1708192187.6779745,
"details": {},
"type": "message",
"subtype": "log"
}

Methods​

getMessages​

â–¸ getMessages(options): Promise<{ data: ConversationChatMessage[], hasNext, hasPrev, nextPage(), prevPage() }>

Returns the list of chat messages for a given addressId.

Parameters​

NameTypeDefault valueDescription
optionsobject-
options.addressId?stringundefinedChat messages exchanged with this particular address id will be fetched.
options.pageSize?number10The amount of messages per pagination page.

Returns​

Promise<{ data: Address[], hasNext, hasPrev, nextPage(), prevPage() }>

Example​

await client.chat.getMessages({ addressId: "9dbe9e94-c797-461e-b5a3-af6d095deaf4" });

subscribe​

â–¸ subscribe(options): {cancel()}

Returns a list of Addresses.

Parameters​

NameTypeDefault valueDescription
optionsobject-
options.addressIdstring-The address to subscribe to
options.onMessage(ConversationEventParams)=>void-The callback that gets called when a new message event happens

Returns​

An object will be returned with a cancel() function which can be used to unsubscribe to the event.

Example​

const { cancel } = await client.chat.subscribe({
addressId: "9dbe9e94-c797-461e-b5a3-af6d095deaf4",
onMessage(msg) {
console.log(msg);
},
});

cancel();

sendMessage​

â–¸ sendMessage(options): Promise<Conversation>

Send a Chat Message to a given Address ID. If no Conversation exists with that Address ID, one will be created. This is the same function as conversation.sendMessage.

Parameters​

NameTypeDescription
optionsobject
options.addressIdstringThe ID of the Address where to send the message.
options.textstringThe Message text content.
options.metadata?objectMetadata to go along with the Message.
options.details?objectExtra Message event details. Can be used to construct custom UIs, for example.

Example​

await client.chat.sendMessage({
addressId: "12345",
text: "Hello from SignalWire!",
});

join​

â–¸ join(options): Promise<JoinConversationResponse>

Joins a conversation given an address without having to send a message. Does nothing if you were already joined to the conversation. This is the same function as conversation.join.

You automatically join a conversation when you send the first message to an address. The join method allows you to join a conversation without first sending a message.

Parameters​

NameTypeDescription
optionsobject
options.addressIdstringThe id of the address to join the conversation of.

Example​

await chat.join({
addressId: "9dbe9e94-c797-461e-b5a3-af6d095deaf4",
});