Messages Overview

Last Edit: Feb 02 2020

Below is a detailed example of how to send a message using Stream Chat:


const message = await channel.sendMessage({
    text: '@Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.',
    attachments: [
        {
            type: 'image',
            asset_url: 'https://bit.ly/2K74TaG',
            thumb_url: 'https://bit.ly/2Uumxti',
            myCustomField: 123
        }
    ],
    mentioned_users: [josh.id],
    anotherCustomField: 234
});
                    

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.");
HashMap<string, object="">extraDataMessage = new HashMap&lt;&gt;();
extraDataMessage.put("anotherCustomField",234);
message.setExtraData(extraDataMessage);

// add an image attachment to the message
Attachment attachment = new Attachment();
attachment.setType("image");
attachment.setImageURL("https://bit.ly/2K74TaG");
attachment.setFallback("test image");
// add some custom data to the attachment
HashMap<string, object=""> extraDataAttachment = new HashMap&lt;&gt;();
extraDataAttachment.put("myCustomField", 123);
attachment.setExtraData(extraDataAttachment);

message.setAttachments(Arrays.asList(attachment));

// include the user ID of the mentioned user
message.setMentionedUsersId(Arrays.asList("josh-id"));

channel.sendMessage(message,
    new MessageCallback() {
        @Override
        public void onSuccess(MessageResponse response) {

        }

        @Override
        public void onError(String errMsg, int errCode) {

        }
    }
);
                    

// First of let's create an extra data for a channel.
struct Product: Codable {
    let id: String
    let name: String
    let price: Int
}

struct AttachmentData: Codable {
    let info: String
}

// Update your extra data types for the decoding.
ExtraData.decodableTypes = [.channel(ChannelInfo.self),
                            .message(Product.self),
                            .attachment(AttachmentData.self)]

// Create an extra data for message.
let iPhone = Product(id: "iPhone12,3", name: "iPhone 11 Pro", price: 999)

// Create an extra data for the attachment.
let fish = AttachmentData(info: "Just a fish to increase sales.")

// Create attachment.
let attachment = Attachment(type: .image,
                            title: "A fish",
                            imageURL: URL(string: "https://bit.ly/2K74TaG")!,
                            extraData: fish)

// Create a message with the extra data and attachments.
let message = Message(text: "We have a new iPhone 11 Pro. Do you want to buy it?",
                      attachments: [attachment],
                      extraData: iPhone,
                      mentionedUsers: [User(id: "josh-id", name: "Josh")])

channel.send(message: message).subscribe().disposed(by: disposeBag)
                    

val message = Message()
message.text =
    "Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish."
val extraDataMessage = HashMap<string, any="">()
extraDataMessage.put("anotherCustomField", 234)
message.extraData = extraDataMessage

// add an image attachment to the message
val attachment = Attachment()
attachment.setType("image")
attachment.setImageURL("https://bit.ly/2K74TaG")
attachment.setFallback("test image")
// add some custom data to the attachment
val extraDataAttachment = HashMap<string, any="">()
extraDataAttachment.put("myCustomField", 123)
attachment.setExtraData(extraDataAttachment)

message.attachments = Arrays.asList(attachment)

// include the user ID of the mentioned user
message.setMentionedUsersId(Arrays.asList("josh-id"))

channel.sendMessage(message,
    object : MessageCallback {
        override fun onSuccess(response: MessageResponse) {
            println(response)
        }

        override fun onError(errMsg: String, errCode: Int) {
            println("$errMsg: $errCode")
        }
    }
)
                    

final message = Message(
  text: '@Josh I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.',
  attachments: [
    Attachment(
      type: "image",
      assetUrl: "https://bit.ly/2K74TaG",
      thumbUrl: "https://bit.ly/2Uumxti",
      extraData: {
        "myCustomField": 123,
      }
    ),
  ],
  mentionedUsers: [
    User(id: "josh")
  ],
  extraData: {
    "anotherCustomField": 234,
  },
);

await channel.sendMessage(message);
                    

// Sending a message from jenny mentioning bob-1

$message = $channel->sendMessage([
	'text' => '@Bob I told them I was pesca-pescatarian. Which is one who eats solely fish who eat other fish.',
	'attachments' =>
		[
			[
				'type' => 'image',
				'asset_url' => 'https://bit.ly/2K74TaG',
				'thumb_url' => 'https://bit.ly/2Uumxti',
				'myCustomField' => 123
			]

		],
	'mentioned_users' => ['bob-1'],
	'anotherCustomField' => 456
	],
	'jenny');
                    

There are five built-in fields for the message:

Name Type Description Default Optional
text string The text of the chat message (Stream chat supports markdown and automatically enriches URLs).
attachments array A list of attachments (audio, videos, images, and text). Max is 10 attachments per message. Each attachment can have up to 5KB.
user object This value is automatically set in client-side mode. You only need to send this value when using the server-side APIs.
mentioned_users array A 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 data object Extra data for the message. Must not exceed 5KB in size.

Note that both the message and the attachments can contain custom fields. By default Stream’s frontend 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:


await client.getMessage(messageID);
                    

client.getMessage(messageId, new MessageCallback(){
    @Override
    public void onSuccess(MessageResponse response) {
        
    }

    @Override
    public void onError(String errMsg, int errCode) {

    }
});
                    

Client.shared.message(with: messageId)
    .subscribe(onNext: { response in
        print(response)
    })
    .disposed(by: disposeBag)
                    

channel.getMessage(messageID, object : MessageCallback {
    override fun onSuccess(response: MessageResponse) {}

    override fun onError(errMsg: String, errCode: Int) {}
}
                    

final message = await client.getMessage("message-id");
                    

$message = $client->getMessage('message-id');
                    

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:


const message = { id: 123, text: 'the edited version of my text' };
const update = await client.updateMessage(message);
                    

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

// send the message to the channel
client.updateMessage(message, new MessageCallback(){
    @Override
    public void onSuccess(MessageResponse response) {

    }

    @Override
    public void onError(String errMsg, int errCode) {

    }
});
                    

let editedMessage = Message(id: message.id, text: newText)
channel.send(message: editedMessage).subscribe().disposed(by: disposeBag)
                    

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

// send the message to the channel
channel.updateMessage(message, object : MessageCallback {
    override fun onSuccess(response: MessageResponse) {}

    override fun onError(errMsg: String, errCode: Int) {}
}
                    

await client.updateMessage(Message(id: "123", text: "the edited version of my text"));
                    

$message = [
	'user_id' => 'jenny',
	'id' => 'message-id',
	'text' => 'the edited version of my text'
];

$update = $client->updateMessage($message);
                    

Delete A Message

You can delete a message by calling removeMessage  and including a message with an ID:


await client.deleteMessage(messageID);
                    

client.deleteMessage("messageID", new MessageCallback(){
    @Override
    public void onSuccess(MessageResponse response) {

    }

    @Override
    public void onError(String errMsg, int errCode) {

    }
});
                    

// Delete a message.
channel.delete(message: message).subscribe().disposed(by: disposeBag)
// or
message.delete().subscribe().disposed(by: disposeBag)
                    

channel.deleteMessage(messageID, object : MessageCallback {
    override fun onSuccess(response: MessageResponse) {}

    override fun onError(errMsg: String, errCode: Int) {}
})
                    

await client.deleteMessage("123");
                    

$delete = $client->deleteMessage('message-id');