ClickCease
Abtract visualisation of real-time messaging

Mastering Real-Time Messaging in React Native with TypeScript

Italo Orihuela
Italo Orihuela
Engineer
Android
iOS
Web
Jan 30, 2024

This blog post will give an introduction to how you can enhance your React Native application with TypeScript chat messages. We will explore the robust messaging features provided by the React Native Chat SDK, including the different types of messages supported and the message creation pattern. We will also discuss the message sending status, which allows developers to monitor the delivery status of their messages.


Pre-requisites
Before we dive into the tutorial, ensure you have the following requirements:

  1. Basic knowledge of TypeScript.
  2. An Amity Social Cloud Portal account
  3. An Amity Social Cloud Console Account
  4. A UI or access to Amity Social Cloud UI Kits

Note: If you haven’t already registered for an Amity Social Cloud account, we recommend following our comprehensive step-by-step guide in the Amity Portal to create your new network.

Step 1: Understanding the Types of Messages

The React Native Chat SDK supports the sending and receiving of six types of messages: text, image, video, audio, file, and custom messages. Each type of message has its own unique properties and methods, allowing you to customize the messaging experience for your users. For instance, a text message can be used for simple communication, an image or video message can be used to share media, an audio message can be used for voice notes, a file message can be used to share documents, and a custom message can be used for any other type of data that you want to send.

Step 2: The Message Creation Pattern

The SDK optimizes the messaging flow by instantly displaying sent messages, even before they have been delivered to the server. This is achieved through the message creation pattern, which starts by optimistically creating a message on the local device without waiting for the server response. This provides a fluid messaging flow, enhancing the user experience. For example, when a user sends a text message, they can see their message in the chat immediately, providing a sense of instant communication.

Step 3: Monitoring Message Delivery Status

The SDK provides the syncState property in the message model to monitor the message delivery status. This property can have one of five states: CREATED, UPLOADING, SYNCING, SYNCED, and FAILED. These states represent the different stages of a message's journey from creation to being successfully sent and synced with the server. This feature can be particularly useful in situations where network connectivity is unstable, as it allows users to understand the status of their messages.

Step 4: Implementing Real-Time Messaging

Now that we understand the basics, let’s discuss how to implement real-time messaging in our ReactNative application with TypeScript. We’ll start by creating a text message. This can be done by creating a new instance of the AmityTextMessageCreateOptions class and passing the necessary parameters.

Next, we’ll monitor the message delivery status. This can be done by calling the getMessage method on the messageRepository object and passing the message ID. The observe method can then be used to monitor changes in the message data, including the syncState.

Final Thoughts
Real-time messaging is a powerful feature that can greatly enhance the user experience of your application. By understanding the types of messages supported by the React Native Chat SDK, the message creation pattern, and how to monitor the message delivery status, you can provide a smooth and instant messaging experience for your users. Moreover, Amity provides a React Native Chat UI Kit that can help you accelerate this development!