Skip to main content
Version: v5

Sending Custom Attachments

The Stream Chat SDK has support for some built-in attachment types like images, videos, and files. However, you can also create your custom attachments that have their own type and contain arbitrary data. You can use this to add location info, custom media like audio, product details, or whatever other content your app deals with to a message.

In this guide, we'll look at how to create custom attachments and send them to a channel.

To render custom attachments with the Compose SDK, see the Custom Attachments page.

Sending an attachment requires you to create the Attachment object, add it to a message, and then send that message:

val attachment = Attachment(...)
val message = Message(
cid = "messaging:general",
text = "Look at this attachment!",
attachments = mutableListOf(attachment),
)
ChatClient.instance().sendMessage(channelType = "messaging", channelId = "general", message = message).enqueue { result ->
if (result.isSuccess) {
// Use result.data()
} else {
// Handle result.error()
}
}

Let's see how you can create an Attachment.

Create an Attachment Without Files

If your attachment is just plain data (for example, location coordinates), and requires no file to be transferred, you can create and send it in the following way:

val attachment = Attachment(
type = "location", // 1
extraData = mutableMapOf( // 2
"lat" to 40.017985,
"lon" to -105.280184,
),
)
  1. Setting a custom value for the type of the attachment makes it easy to identify the custom attachment on the receiving side to render it in the Message List.
  2. The extraData Map allows you to add arbitrary pieces of data to the attachment. In this example, we use this to add a pair of location coordinates.

Create an Attachment With Files

If you want to upload a file for your attachment, you need to create an Attachment object that has its upload property set:

val attachment = Attachment(
type = "audio", // 1
upload = File("audio-file.mp3"), // 2
)
  1. Again, a custom type allows you to identify your custom attachments to render them in the Message List.
  2. The file in the upload property will be uploaded automatically before the message is sent. The URL where it's available from will be placed in the url property of the Attachment.

By default, files are uploaded to Stream's CDN, which has a 100 MB size limit. However, you can also use your own CDN for files.

Did you find this page helpful?