One-On-One Chat With Ruby on Rails and React Native

One of the fastest ways to build applications has always been Ruby on Rails. Ruby on Rails is a feature-packed web development framework that makes it easy for novices and experts alike to easily stand up an application. When you want to add chat to your application, using Stream Chat makes that task just as easy, and it's quick to do!

In this tutorial, I will describe how to add chat to a React Native application powered by a Ruby on Rails backend.

Gif of Finished Application

As always, the full code can be found on GitHub.

Prerequisites

To be successful in building a chat app using Ruby on Rails, you'll want to be sure you have the following before proceeding:

Setting Up the Project

In this tutorial, we'll be building a server (frontend) and a client (backend). You'll need to create a directory to house both of these sections and then cd into it to install the required dependencies for each of them. All of this can be accomplished using the following commands:

https://gist.github.com/adelowo/b3f552557964a2ad8d883956bb4255bd

Building the Server

The server will expose only one endpoint: /users. This endpoint will accept a username and password which will be validated against the database. If the username doesn’t exist in the database, it regards it as a new user and adds it. As a final step, it connects to Stream Chat and generates a token that will be used client-side.

The first step is to set up the required files and add dependencies such as bcrypt and stream-chat SDK. You can do that by running the following code:

https://gist.github.com/adelowo/f258f32074e9d644a1cbe15fd660df1e

Once everything above succeeds, the next step is to update the generated controller to include the logic for authenticating and creating users. You will need to update the contents of the file located at app/controllers/users_controller.rb with the following code:

https://gist.github.com/adelowo/240c6da63fa3bfafd3b40e62fee88e03

The next step is to add validation requirements to the User class so that we are certain only safe data is written to the database. This can be done by updating app/models/user.rb with the following:

https://gist.github.com/adelowo/5510197530551c7a0dbe432832066644

The next step will be to connect the controller to a route. Rails has a file that handles all routings, located at config/routes.rb. That file needs to be updated to contain the following:

https://gist.github.com/adelowo/d02c273b3eada71529cc8f853664b01d

As a final step, you will need to handle environment variables to safely pass the API key and secret from Stream Chat into the app. If you notice, we already installed a dotenv gem that is going to help read a .env file and populate the variables available to the app. Create a .env file by running:

$ touch .env

In the newly created .env file, add the following content:

API_KEY=your_API_KEY
API_SECRET=your_API_SECRET

Please remember to replace "your_API_KEY" and "your_API_SECRET" with the values from your Stream Dashboard.

Once this has been created and populated, you will need to load the .env file when the app boots up. We can ensure this by editing config/application.rb to contain the following:

https://gist.github.com/adelowo/29bb8df1065231766c8f7e27730e4ea9

That's it! You can now start the server by running the following command:

$ rails server

Building the Client

The first step to building our front end is to add a few more required dependencies and files to the client application. That can be done using the following commands:

https://gist.github.com/adelowo/d868dbb87b779f56d16ab03542446403

In the newly created Chat.js file, paste the following:

https://gist.github.com/adelowo/41a13facd905601f63fb3c44ae1be26c

The next step is to update Auth.js to contain the logic for user authentication. That can be done by amending it with the following code:

https://gist.github.com/adelowo/c74be27e9a70767b72c299587aa23b85

The final step will be to update App.js to contain the code below:

https://gist.github.com/adelowo/55d87876bc92174a092b75c0853c7551

Our front end is now ready! To run the app, execute the following command:

$ yarn run ios

Wrapping Up

In this tutorial, I have described how to add a chat interface to your Ruby on Rails app using Stream Chat. This can be taken further by implementing a social network with Rails that supports direct messaging; checkout the Stream Chat docs to learn about all the awesome updates you can make to your application.

Thanks for reading, and happy coding!

TutorialsChat