Skip to content

Conversation

vladvelici
Copy link
Contributor

Description

Add an article showing how to add image (or other files) sharing in Ably Chat. It's a high-level overview, I don't think we should go into more details.

The URL is /docs/guides/chat/media-sharing. We previously discussed not to put it as a guide so I'll move it where we think it should be and add a link in the side menu.

Checklist

Copy link

coderabbitai bot commented Jun 24, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chat-image-sharing-doc-CHA-965-CHA-966

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -0,0 +1,160 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these pages should go in the main docs section, rather than the guides - as the intention is to cover off features that are trivial to implement rather than a use-case guide (it's slightly misleading but the guides folder is for wider chat-use cases :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to rooms/

title: "Sharing media with Ably Chat"
meta_description: "How to share media like images, videos, or files with Ably Chat"
meta_keywords: "media, sharing, Ably Chat, chat SDK, realtime messaging, dependability, cost optimisation"
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page needs to be added to src/data/nav/chat.ts for it to show up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


We'll build a simple chat room where users can share images with each other.

We also assume that `room` is an Ably chat room that is attached. Please refer to our getting started guide for step-by-step instructions on how to set up Ably Chat and get a chat room.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest we include a link here to the JS GS guide

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


We also assume that `room` is an Ably chat room that is attached. Please refer to our getting started guide for step-by-step instructions on how to set up Ably Chat and get a chat room.

We assume that a function called `uploadImage() : Promise<string>` exists and handles the whole image upload flow: asks users to pick a picture, uploads it, and returns a unique identifier for this image.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one above says "also assume", should it be the other way around?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

let imageId = 'abcd123abcd'; // assume this is the image we want to remove
let message = ...; // assume this is the message we want to edit

if (!message.metadata.images || message.metadata.images.length === 0) { return; /* do nothing if no images */}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put the return on a new line, looks cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


Our moderation integration is currently limited to text moderation. To add automatic or human moderation for images (and other attachments) you must implement moderation on the server side. A recommended flow is:

1. Upload the media to your storage service (can be S3 or others)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest making this a synchronous process - avoids race conditions, and usually uploading images you'd accept that it takes a bit longer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send message and then upload pictures? Or start both and edit the message when the pictures are ready?

I wanted to avoid the edit-loop where you need to edit the message to attach images. It is a valid flow especially for moderation-before-publish, where image metadata can be sent without image (so UI can display a "processing" message or something) and an edit adds the image links post-moderation. The initial message gets sent before uploads finish. Potentially start the uploads after message is sent so you have a serial and the edit happens server-side?

@AndyTWF
Copy link
Contributor

AndyTWF commented Jun 25, 2025

Generally speaking, I would suggest that instead of saying we/our, say "Ably Chat"

```javascript
const imageIdRegex = /^[a-z0-9]{10,15}$/;

function getValidImages(message) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include the type here for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for JS instead of TS but happy to swap and add types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeh, if the tag is js then that's fine :)

```javascript
const imageIdRegex = /^[a-z0-9]{10,15}$/;

function getValidImages(message) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding the expected type here so users know this is for a Message?


Instead, add an authetnication layer on the server side to control access to the media. This allows you full control over who can access the files.

If you must serve the files directly form a service such as AWS S3 consider saving a file name in the metadata of the message, and have a mechanism for the user to request a signed URL to the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you must serve the files directly form a service such as AWS S3 consider saving a file name in the metadata of the message, and have a mechanism for the user to request a signed URL to the file.
If you must serve the files directly form a service, such as AWS S3, consider saving a file name in the metadata of the message, and have a mechanism for the user to request a signed URL to the file.

@vladvelici vladvelici force-pushed the chat-image-sharing-doc-CHA-965-CHA-966 branch from 1444c1d to 6a80a18 Compare June 30, 2025 14:55
@vladvelici
Copy link
Contributor Author

Thanks @splindsay-92 and @AndyTWF for the review. I've made all the changes (one question remaining).

I'll rebase the commits before merging. Would you be able to have another look please?

Copy link
Contributor

@AndyTWF AndyTWF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with the overall content - as this is the first guide of this type, would be good to get some @m-hulbert feedback

@vladvelici vladvelici force-pushed the chat-image-sharing-doc-CHA-965-CHA-966 branch from 6a80a18 to fb214ba Compare July 4, 2025 01:19
@@ -84,6 +84,10 @@ export default {
name: 'Room reactions',
link: '/docs/chat/rooms/reactions',
},
{
name: 'Media sharing',
link: '/docs/chat/rooms/media-sharing',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this really go at the rooms level? Do we think its likely we may have other docs, perhaps we should group them under some directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find it odd tbh.

Just synced with @AndyTWF on this, and the idea is to leave it here for now, but we'll probably move it later as we'll reorganise the docs a little bit.


When the user initiates the upload flow (for example by clicking a button called "attach"), we use the `uploadImage()` flow and save the resulting ID, like this:

```javascript
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right in the guide, I think you need to wrap this with tags

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<Code> tags needed here, yep.


Ably Chat currently does not support media sharing out of the box, but it is very straightforward to link media and metadata to messages, and this can be used to implement media sharing features.

## Architecture Overview
Copy link
Contributor

@splindsay-92 splindsay-92 Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these headings need the new <a id="architecture-overview"/> wrapping?

@vladvelici vladvelici requested a review from m-hulbert July 7, 2025 12:51
Copy link
Contributor

@m-hulbert m-hulbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a few suggestions on formatting and terminology for consistency. Happy to help with this if needed.


A common requirement for chat applications is the ability to share media like images, videos, or files with other participants.

Ably Chat currently does not support media sharing out of the box, but it is very straightforward to link media and metadata to messages, and this can be used to implement media sharing features.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This starts out a bit negative. Do we really need to state that we don't have a dedicated API for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to a more positive tone, not mentioning we don't support it

meta_keywords: "image, file, media, sharing, Ably Chat, chat SDK, realtime messaging, dependability, cost optimisation"
---

A common requirement for chat applications is the ability to share media like images, videos, or files with other participants.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A common requirement for chat applications is the ability to share media like images, videos, or files with other participants.
A common requirement for chat applications is the ability to share media such as images, videos, or files.

'with others' is implicit in the sharing.

@@ -0,0 +1,165 @@
---
title: "Sharing media with Ably Chat"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be "Media sharing" to match with the nav and the format of other pages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks


Ably Chat currently does not support media sharing out of the box, but it is very straightforward to link media and metadata to messages, and this can be used to implement media sharing features.

## Architecture Overview
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence case for headings please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks


The general approach for implementing media sharing in Ably Chat is:

1. Upload media to your own storage service (your own servers, or a third-party service like AWS S3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Upload media to your own storage service (your own servers, or a third-party service like AWS S3)
1. Upload the media to your own storage service. This can be your own servers, or a third-party service like AWS S3.

}
```

In the UI, the `imagesToAttach` array is displayed so the users know what images will be attached to their message. The UI can allow users to remove or sort the images.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the UI, the `imagesToAttach` array is displayed so the users know what images will be attached to their message. The UI can allow users to remove or sort the images.
In your UI, the `imagesToAttach` array should be displayed so that users know which images will be attached to their message. It can allow users to remove or sort the images.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied your version, thanks!

Comment on lines 100 to 102
### Metadata for each image

In the example above we used a simple string array to store image IDs. We can change this to be an object where each image holds extra information that can be displayed to the user, such as a title or description, or the expected size in pixels so that the UI can allocate a placeholder loading box of the correct proportions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be titled differently and provide an example?

Copy link
Contributor Author

@vladvelici vladvelici Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this section and instead made it the default.


## Privacy and security considerations

The message metadata is not validated by the server. Always treat it as untrusted user input. The same applies to the text and headers of a chat message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a note when we first mention using the metadata field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved


### Validate metadata on the receiving end

You must always validate the metadata before displaying any media to the user. Treat the metadata as untrusted user input. If you are using IDs, validate they are of the correct format. If you are using URLs, validate the schema, domain, path and query parameters are what you expect. Do not display images or other media directly from the metadata of a message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems like something to mention within the "Display images/attachments" section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved


You must always validate the metadata before displaying any media to the user. Treat the metadata as untrusted user input. If you are using IDs, validate they are of the correct format. If you are using URLs, validate the schema, domain, path and query parameters are what you expect. Do not display images or other media directly from the metadata of a message.

### Use authentication server-side to control access
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be higher up the page? Seems important to understand the access controls before setting it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to leave it towards the end since we don't really talk at all about the server-side stuff. I can't find a way to fit it in nicely with the rest of the article.

@vladvelici vladvelici force-pushed the chat-image-sharing-doc-CHA-965-CHA-966 branch from fb214ba to e755f90 Compare July 16, 2025 13:45
@vladvelici
Copy link
Contributor Author

Thanks @m-hulbert for the feedback, very useful, I've changed it quite a bit now.

Copy link
Contributor

@splindsay-92 splindsay-92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good!

@m-hulbert m-hulbert added the review-app Create a Heroku review app label Jul 18, 2025
@ably-ci ably-ci temporarily deployed to ably-docs-chat-image-sh-l6wt47 July 18, 2025 09:43 Inactive
Copy link

@david-hernandez-ably david-hernandez-ably left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice article, I find it a bit complex, but I think it can be valuable. Compare this complexity with how you send a file in Sendbird:

const {
    requestId,
    url,
} = await channel.uploadFile({
    file,
    uploadStartedHandler: (requestId: string) => {
        // upload got started
    },
    progressHandler: (requestId: string, progress: number, total: number) => {
        // progress in percentage = progress / total
    },
});
channel.sendFileMessage({
    fileUrl: url,
    ...
})
...
.onSucceeded((message: FileMessage) => {
    // message is sent successfully
    ...
})

In my opinion, sending files is a basic feature for 1-1 and group chat we should offer in our SDK, because it adds a lot of value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-app Create a Heroku review app
Development

Successfully merging this pull request may close these issues.

6 participants