Translate Chat Messages in Real Time with Amazon Translate

5 min read
Ayooluwa I.
Ayooluwa I.
Published April 27, 2020 Updated May 12, 2020

Language barriers can hinder growth and build a wall between your business and its potential customers, ultimately limiting your market and costing you money. If a customer reaches out to you by chat in German or Spanish, but you don’t understand the language, wouldn't it be nice to still be able to win them over?!

Just because your customers don’t speak the same language(s) as your team does, it doesn’t mean they should be cut off from interacting with your brand. It is possible to provide realtime language translation features in your chat app so that you can understand queries from speakers of other languages and provide a prompt response in your native language, which is also translated for the customer!

This tutorial will take you through building a chat application where users can select their preferred language, and have messages sent to them translated to that language in real time. Here’s a screenshot of the application, the code for which can be found on GitHub:

Screenshot of Finished Translating Chat


Before you proceed with the rest of this tutorial, make sure you have Node >= 8.10 and yarn >= 1.22 installed in your machine. You'll also need a basic understanding of JavaScript and React, so that you’ll be able to understand the code we’ll be writing.

Signing Up for Amazon AWS

If you don’t already have an account, you can register for a free AWS account here ( otherwise, you can log in to your existing account). Once you’ve completed the signup process, wait for your account to be activated before proceeding.

Once activated, log into your AWS account, click your username at the top right corner, then click My Security Credentials. Next, click Users on the sidebar. You will be redirected to a page where you can view, add or delete users on your account. Click the Add user button, assign a User name to the user, and tick Programmatic access under Access type:

Screenshot of the Set user details Step

Next, under Set permissions, click Attach existing policies directly and then select TranslateFullAccess under Filter policies.

Screenshot of the Set permissions Step

You can skip the optional Tags step, and go ahead to create the user on step four of the user creation process in AWS. Once the user is created, you will be provided with the Access key ID and Secret access key for that user. Keep this page open until we copy the keys to a .env file, which we’ll create shortly.

Signing Up for Stream

Follow this link to create a new Stream account, or sign in to your existing account. Once you’re redirected to the dashboard, create a new app and take note of the application access keys which will be presented to you on creation:

Screenshot of App Access Keys in the Stream Dashboard

Setting Up the Server

Create a new directory for this project in your filesystem and cd into it. Then, run yarn init -y from the project root to initialize a new Node.js project. Following that, run the command below to install all the dependencies that are needed to build the server:

$ yarn add express dotenv cors aws-sdk stream-chat body-parser

which installs:

  • stream-chat: The Stream chat library
  • express: Node.js web application framework
  • body-parser: Node.js body parsing middleware
  • dotenv: For loading environmental variables from a .env file into process.env
  • cors: Node.js CORS middleware
  • aws-sdk: The official AWS SDK for the browser and Node.js

Next, create a new .env file in the project root and add the following environmental variables to it, replacing the placeholders with the appropriate values from your Stream and AWS dashboards:

STREAM_API_KEY=<your stream api key>
STREAM_APP_SECRET=<your stream app secret>
AWS_ACCESS_KEY_ID=<your aws access key>
AWS_SECRET_ACCESS_KEY=<your aws access secret>
Building your own app? Get early access to our Livestream or Video Calling API and launch in days!

Save the .env file, then go ahead and create a new server.js file in the project root. Open it in your text editor, and paste in the following code:


const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { StreamChat } = require('stream-chat');
const AWS = require('aws-sdk');
const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

const serverSideClient = new StreamChat(

The /users route is where the creation of users on our Stream instance takes place, while the /translate route is where the text sent by the chat client is converted to the language specified by the lang variable through Amazon Translate’s translateText method. The translation is subsequently sent back to the client to be displayed to the user.

That’s all we need to do on the server! You can start it on port 5500 by running node server.js in the terminal.

Setting Up the React Application

Within your project directory, use the create-react-app CLI to bootstrap a new React application using the command below.

$ npx create-react-app client

Once the app has been created, cd into the new client directory and install the following additional dependencies that we’ll be needing throughout the course of building the application frontend:

$ yarn add stream-chat stream-chat-react random-username-generator axios

which installs:

Once the installation is complete, start your development server by running yarn start from within the client directory, then navigate to http://localhost:3000 in your browser to view the app.

Building the Chat UI

Open up client/src/App.js in your text editor and update it to look like the snippet below:

import React from 'react';
import {
} from 'stream-chat-react';
import rug from 'random-username-generator';

This is all the code we need to get our application looking and functioning as it should! Between lines 103 and 157, you can see how various components provided by the stream-chat-react package are used to construct a feature-rich and responsive user interface. Here’s what each component does:

  • Chat acts as a wrapper and provides ChatContext to all other components.
  • ChannelList displays a preview list of channels, allowing you to select the channel you want to open.
  • Channel acts as a wrapper component for a channel.
  • To render basic information about a channel, the list of messages in the channel and the text input, the CustomChannelHeader, MessageList, and MessageInput components are used, respectively.

Lines 60-84 are where language translation happens. When messages are sent to the channel, the ones sent from the current user are appended to the messages array immediately and are subsequently displayed on the screen. Messages from other users are sent to the /translate endpoint on the server with the lang set to whatever the user has selected in the UI. Once the translated text is received on the client, it is displayed to the user.

You can see this in action in the screenshot below. The first user sends messages in English and receives responses in English, while the second sends messages in French and receives in French, as well.

Screenshot of Finished Translating Chat App

Wrapping Up

In this article, we discussed why it is important to provide language translation features in a chat app, and then combined the services of Stream Chat and Amazon Translate to build a functioning demo.

Be sure to check out the documentation for both services to learn more about all the features and integrations that are available to you.

Thanks for reading, and happy coding!

decorative lines
Integrating Video With Your App?
We've built an audio and video solution just for you. Launch in days with our new APIs & SDKs!
Check out the BETA!