Get message details

Get message details, retrieve content, and check if the message was deleted.

Get message details​

getMessage() fetches the Message object from Message Persistence based on the message timetoken.

Method signature​

This method takes the following parameters:

1channel.getMessage(
2 timetoken: string
3): Promise<Message>

Input​

* required
ParameterDescription
timetoken *
Type: string
Default:
n/a
Timetoken of the message you want to retrieve from Message Persistence.

Output​

TypeDescription
Promise<Message>Returned Message object.

Sample code​

Get the message with the 16200000000000001 timetoken.

1// reference the "incident-management" channel
2const channel = await chat.getChannel("incident-management")
3// get the timetoken of the last message on the channel
4const messageTimetoken = (await channel.getHistory({count: 1})).messages[0].timetoken
5// return the message object
6const message = await channel.getMessage("16200000000000001")

Get historical details​

With Message Persistence enabled, PubNub stores all historical message data, metadata, and actions.

Fetch historical message details with getHistory(). By default, all message actions and metadata are included in the response.

Get message content​

Access the text content of a Message object using the text property.

Method signature​

This method has the following signature:

1message.text: string

Properties​

PropertyDescription
text
Type: string
Text content of the returned message.

Sample code​

Get the content of the message with the 16200000000000000 timetoken.

1// reference the "message" that you're interested in
2const message = await channel.getMessage("16200000000000000")
3
4// get message content
5message.text

Check deletion status​

Check if a Message object was soft-deleted using the deleted property.

Method signature​

This method has the following signature:

1message.deleted: boolean

Properties​

PropertyDescription
deleted
Type: boolean
Info on whether the message has the deleted status (because it was previously soft-deleted) or not.

Sample code​

Get the status of the message with the 16200000000000000 timetoken.

1// reference the "message" that you're interested in
2const message = await channel.getMessage("16200000000000000")
3
4// get message deletion status
5message.deleted

Was this page useful?

Last updated on