•12 months ago
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.
As always, the full code can be found on GitHub.
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:
Building the Server
The server will expose only one endpoint:
/users. This endpoint will accept a
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
stream-chat SDK. You can do that by running the following code:
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:
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:
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:
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:
1$ touch .env
In the newly created
.env file, add the following content:
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:
That's it! You can now start the server by running the following command:
1$ 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:
In the newly created
Chat.js file, paste the following:
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:
The final step will be to update
App.js to contain the code below:
Our front end is now ready! To run the app, execute the following command:
1$ yarn run ios
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!