React Introduction

LAST EDIT Oct 22 2021

This document covers the resources and methods provided by the Javascript Chat Client that is included with the React UI SDK for Stream Chat.

Background

Copied!

Stream Chat integrations running in the front end have two components:

  • The Chat UI SDK: A set of UI components to present information from the chat API to the user and collect user input. You can find the full docs for the UI SDK here.

  • The Javascript Chat Client: A Chat Client to server as an interface between the Chat UI and Stream Chat's REST API.

If you are building Stream Chat into your React app, we recommend using the UI SDK if at all possible. Let us tell you from experience, building a chat UI that has the features mature chat applications like Slack, Facebook messenger, or Discord have is not easy. Chat applications have a lot of moving parts and the Chat Component SDK handles all the stateful chat logic for you to make integration easy. Also, the components in the React SDK are customizable and let you to incorporate your chat application into your existing application experience.

You may find that, for various reasons, you may need to build one or more of your chat UI components yourself. In that case, you can use the Chat API client instead of the Chat Component SDK.

Installation

Copied!

The React SDK includes the Javascript chat client by default and, if you follow the instructions for installing the React SDK, you have installed the Javascript client. However, if you want to just install the Javascript client, you can do so with the following steps.

You're currently not logged in. Create an account to automatically add your API key and secret to these code examples.

You can install the chat client using either npm or Yarn, depending on your preference.

After installing the package, import the StreamChat module into your project and you're ready to go:

Getting Started

Copied!

Now that you're logged in, let's quickly get up to speed on using Stream’s Chat API before you start connecting the client to your React application.

Clients and Users

Copied!

The first step to using the chat client is initializing a client and setting the current user:

The above snippet is for an in-browser or mobile integration. The server side version of the client works a little bit differently. That is covered in detail in the documentation for whichever server side SDK you wind up using so don't worry about it for now.

Please also note here the usage of StreamChat.getInstance() available since stream-chat-js@2.1.2. This new Singleton pattern allows you to instantiate a unique StreamChat client, i.e create a StreamChat instance and retrieve it wherever you need it on your app to perform API calls. After calling it once, any following getInstance() call will return the initial StreamChat instance. This should prevent from accidentally creating multiple StreamChat instances.

There's more to authenticating users that gets covered in the main client docs.

Channels

Copied!

Let’s continue by initializing your first channel. A channel contains messages, a list of people that are watching the channel, and optionally a list of members (for private conversations). The example below shows how to set up a channel to support chat for a group conversation:

As shown in code-snippet above, you can initialise a channel either by providing

  • ID for channel

  • OR list of members for channel. In this case, id will be auto-generated on backend for the channel. Please note that, you can't add or remove members from channel created using members list .

The channel type controls the settings we’re using for this channel.

There are 5 default types of channels:

  • livestream
  • messaging
  • team
  • gaming
  • commerce

These five options above provide you with sensible defaults for those use cases. You can also define custom channel types if Stream Chat defaults don’t work for your use-case.

The third argument is an object containing the channel data. You can add as many custom fields as you would like as long as the total size of the object is less than 5KB.

We go into much more detail about the operations available in the Channel docs.

Messages

Copied!

Now that we have the channel set up, let's send our first chat message:

Similar to users and channels, the sendMessage method allows you to add custom fields. When you send a message to a channel, Stream Chat automatically broadcasts to all the people that are watching this channel and updates in real-time. You can also add attachments to the message, please check our Messages docs for details.

Events

Copied!

This is how you can listen to events on the client-side and update the UI for a channel dynamically:

You can receive the event and access the full channel state via channel.state.

Please read more about events at Event docs.

What Next

Copied!

Now that you have a basic understanding of the core components of the Chat API, you can increase your understanding of how the UI SDK and Chat Client work together to add chat to your application by looking at: