Messages Overview

Let’s dive right into it, the example below shows how to send a simple message using Stream:

// Android SDK
ChannelClient channelClient = client.channel("messaging", "general");
Message message = new Message();
message.setText("Josh, I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.");

channelClient.sendMessage(message).enqueue(result -> {
  if (result.isSuccess()) {
    Message sentMessage = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Message.send(type, id)
  .message(
    MessageRequestObject.builder()
      .text(
        "@Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.")
      .userId(userId)
      .build())
  .request();

Note how server side SDKs require that you specify user_id to indicate who is sending the message. You can add custom fields to both the message and the attachments. There’s a 5KB limit for the custom fields. File uploads are uploaded to the CDN so don’t count towards this 5KB limit.

nametypedescriptiondefaultoptional
textstringThe text of the chat message (Stream chat supports markdown and automatically enriches URLs).
attachmentsarrayA list of attachments (audio, videos, images, and text). Max is 30 attachments per message. The total combined attachment size can’t exceed 5KB.
user_idobjectThis value is automatically set in client-side mode. You only need to send this value when using the server-side APIs.
mentioned_usersarrayA list of users mentioned in the message. You send this as a list of user IDs and receive back the full user data.
message custom dataobjectExtra data for the message. Must not exceed 5KB in size.
skip_pushbooldo not send a push notificationfalse

Complex Example

A more complex example for creating a message is shown below:

// Android SDK
// Create an image attachment
Attachment attachment = new Attachment();
attachment.setType("image");
attachment.setImageUrl("https://bit.ly/2K74TaG");
attachment.setThumbUrl("https://bit.ly/2Uumxti");
attachment.getExtraData().put("myCustomField", 123);

// Create a message with the attachment and a user mention
Message message = new Message();
message.setText("@Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.");
message.getAttachments().add(attachment);
message.setMentionedUsersIds(Arrays.asList("josh-id"));
message.getExtraData().put("anotherCustomField", 234);

// Send the message to the channel
channelClient.sendMessage(message).enqueue(result -> { /* ... */ });

// Backend SDK
Message.send(type, id)
  .message(
    MessageRequestObject.builder()
      .text(
        "@Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.")
      .attachment(
        AttachmentRequestObject.builder()
          .type("image")
          .assetURL("https://bit.ly/2K74TaG")
          .thumbURL("https://bit.ly/2Uumxti")
          .additionalField("myCustomField", 123)
          .build())
      .mentionedUsers(Arrays.asList(josh.getId()))
      .additionalField("anotherCustomField", 234)
      .userId(userId)
      .build())
  .skipPush(true)
  .request();

mentioned_users field must contain a maximum of 25 items.

By default Stream’s UI components support the following attachment types:

  • Audio
  • Video
  • Image
  • Text

You can specify different types as long as you implement the frontend rendering logic to handle them. Common use cases include:

  • Embedding products (photos, descriptions, outbound links, etc.)
  • Sharing of a users location

The React tutorial for Stream Chat explains how to customize the Attachment component.

Get a Message

You can get a single message by its ID using the getMessage call:

// Android SDK
channelClient.getMessage("message-id").enqueue(result -> {
  if (result.isSuccess()) {
    Message message = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Message.get(messageId).request();

Get a Message Options

nametypedescriptiondefaultoptional
show_deleted_messagebooleanif true, returns the original messagefalse

show_deleted_message is exposed for server-side calls only.

Update a Message

You can edit a message by calling updateMessage and including a message with an ID – the ID field is required when editing a message:

// Android SDK

// Update some field of the message
message.setText("my updated text");

// Send the message to the channel
channelClient.updateMessage(message).enqueue(result -> {
  if (result.isSuccess()) {
    Message updatedMessage = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Message message = Message.get("123").request().getMessage();
MessageRequestObject messageRequestObject = MessageRequestObject.buildFrom(message);
messageRequestObject.setText("the edited version of my text");
Message.update(message.getId()).message(messageRequestObject).request();

Partial Update

A partial update can be used to set and unset specific fields when it is necessary to retain additional data fields on the object. AKA a patch style update.

Message.partialUpdate(message.getId())
  .setValue("text", "this message just got partially updated")
  .setValue("color", "red")
  .setValue("details.status", "pending") // nested object
  .user(userRequestObject)
  .request();

Message.partialUpdate(message.getId())
  .unsetValue("color")
  .user(testUserRequestObject)
  .request();

Delete A Message

You can delete a message by calling deleteMessage and including a message with an ID. Messages can be soft deleted or hard deleted. Unless specified via the hard parameter, messages are soft deleted. Be aware that deleting a message doesn’t delete its attachments. See the docs for more information on deleting attachments.

// Android SDK
channelClient.deleteMessage("message-id", false).enqueue(result -> {
  if (result.isSuccess()) {
    Message deletedMessage = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Message.delete(messageId)
  .hard(true) // Optionally do a hard-delete
  .request();

Soft delete

  1. Can be done client-side by users

  2. Message is still returned in the message list and all its data is kept as it is

  3. Message type is set to “deleted”

  4. Reactions and replies are kept in place

  5. Can be undeleted

Hard delete

  1. Can be done client-side by users but be cautious this action is not recoverable

  2. The message is removed from the channel and its data is wiped

  3. All reactions are deleted

  4. All replies and their reactions are deleted

By default messages are soft deleted, this is a great way to keep the channel history consistent.

Undelete a message

A message that was soft-deleted can be undeleted. This is only allowed for server-side clients. The userID specifies the user that undeleted the message, which can be used for auditing purposes.

Messages can be undeleted if:

  • The message was soft-deleted

  • The channel has not been deleted

  • It is not a reply to a deleted message. If it is, the parent must be undeleted first

  • The user that undeletes the message is valid

await client.undeleteMessage(messageID, userID);
© Getstream.io, Inc. All Rights Reserved.