Home
/
UIKit
/
React
/
Open channel

File sharing

Users can send files such as images, audio files and videos while chatting in a channel. There are currently four different types of file messages that users can send in UIKit for React: image file, video file, audio file, and document file. These files can be sent to other participants in the channel by uploading them from the sender's device.

In order to send a file message, you must first configure to display channel messages using the OpenChannel component. To learn how to render a custom view of OpenChannel, refer to Chat in a channel.

Showing different types of file messages.

You can customize the UI for file sharing by using renderCustomMessage as shown in the code below.

Light Color Skin
Copy
import {
SendBirdProvider,
    OpenChannel,
    useSendbirdStateContext,
    sendBirdSelectors
} from "sendbird-uikit";
import "sendbird-uikit/dist/index.css";

const CHANNEL_URL = "...";
const APP_ID = "...";
const USER_ID = "...";

const CustomInput = () => {
    const state = useSendbirdStateContext();
    const sdk = sendBirdSelectors.getSdk(state);
    const sendFileMessage = sendBirdSelectors.getOpenChannelSendFileMessage(
        state
    );
    return (
        <div>
            <input
                type="file"
                id="custom-input"
                onChange={() => {
                    const params = new sdk.FileMessageParams();
                    // Do the required manipulations.
                    params.file = document.querySelector("#custom-input").files[0];
                    sendFileMessage(CHANNEL_URL, params);
                }}
            />
        </div>
    );
};

const MyApp = () => {
    return (
        <SendBirdProvider
            appId={APP_ID}
            userId={USER_ID}
        >
            <div style={{ height: "100vh" }}>
                <OpenChannel
                    channelUrl={CHANNEL_URL}
                    renderCustomMessage={(message, channel) => {
                        if (message.messageType === "file") {
                            return () => (
                                <div style={{ color: "red" }}>
                                    {message.plainUrl || message.url}
                                </div>
                            );
                        }
                    }}
                    renderMessageInput={() => {
                        return <CustomInput />;
                    }}
                />
            </div>
        </SendBirdProvider>
    );
};

export default MyApp;

Image compression

UIKit for React allows users to compress images when sending them to other users in the channel on a client app. By reducing the image size, they can send and receive image files faster and minimize the usage of data storage as well as the data usage. Image compression can be applied to the following image types: jpg, jpeg, and png.

Use the imageCompression prop in either the App or SendBirdProvider components.

PropertyTypeDescription

compressionRate

number

The value of the compression rate to apply to the image. Acceptable values are 0.0 to 1.0, inclusive. (Default: 1.0)

resizingWidth

number or string

The new width to apply to the image, in pixel. Acceptable values are either number or string, and when it's string, it should be specified in px.

resizingHeight

number or string

The new height to apply to the image, in pixel. Acceptable values are either number or string, and when it's string, it should be specified in px.

To compress an image without changing the width and height, use the compressionRate property. To resize the width and height of an image, use the resizingWidth and resizingHeight properties.

If you assign values to compressionRate, resizingWidth, and resizingHeight simultaneously, the image will be both compressed and resized. However, for png images, if the values for all three properties are given, only resizingWidth and resizingHeight will be applied to create a compressed image. It is recommended to only assign values to resizingWidth and resizingHeight when compressing a png image.

Light Color Skin
Copy
import {
    App,
    SendBirdProvider,
} from "sendbird-uikit";
import "sendbird-uikit/dist/index.css";
const Example1 = () => {
    ...
    return (
        <App
            appId={appId}
            userId={userId}
            imageCompression={{
                compressionRate: 0.5,
                resizingWidth: 200,
                resizingHeight: '200px',
            }}
        />
    );
}
const Example2 = () => {
        ...
    return (
        <SendBirdProvider
            appId={appId}
            userId={userId}
            imageCompression={{
                compressionRate: 0.5,
                resizingWidth: 200,
                resizingHeight: '200px',
            }}
        >
        ...
        </SendBirdProvider>
    );
}