Build a Real-Time Chat Application With Modulus and Laravel 5

In this tutorial, I will show you how to implement a real-time chat application with Laravel 5, PostgreSQL, and Pusher. Then we will deploy this application to Modulus together.
We will use Laravel 5 for the back-end service, HTML5 and jQuery for a simple front-end application, PostgreSQL for the database, and Pusher for real-time communication between the server and clients. The overall architecture will be like this:






The Scenario

  1. A user opens the chat application in a browser and provides a nickname to continue the chat.
  2. The user enters some text and clicks the Send button.
  3. The message will be handled by a service written using Laravel 5, and it will be persisted to the database. 
  4. The persisted message will be sent to Pusher in order to trigger a new message event to broadcast that message to connected clients.
  5. The new message will be acquired by the clients, and the chat message list will be refreshed for all connected clients. 
We will cover very useful topics with this scenario, even if this is a very simple application.

Environment Preparation

Laravel Project Setup

Let's install Laravel first, so that we can write a chat service for our application. We will use Composer to install Laravel and related packages easily. Please refer to the Composer website to learn more about Composer installation. After installing Composer, open up a command line prompt and run the following command to install Laravel 5:
composer global require "laravel/installer=~1.1"
You will see output like the following:






We are ready to generate a Laravel project. Run the following code to generate a project structure for the chat application.
laravel new RealtimeChatLaravel
This will generate a boilerplate Laravel project, and you will see the following folder structure:






Database

Our application will interact with a database, and it will be PostgreSQL. In this project, we will use ElephantSQL, which is a company that provides PostgreSQL as a Service. You can use several types of database with Laravel, like SQLite, MySQL, PostgreSQL, and SQL Server. I have chosen PostgreSQL because when we deploy our project to Modulus, you will not be able to use an internal database like the above database types. I prefer to use a database which provides it as a service. ElephantSQL allows you to try out some of the good features of PostgreSQL with a free plan. 
You can go and grab a free plan from ElephantSQL to use for your needs. When you've finished your account and database creation, you will know database information like HostnameDatabase name, Username, and Password. Please write down that information to use in Laravel for database configuration.

Pusher

This company provides a service to trigger events for real-time communication. You can go the Pusher website to get one. After successful account and application creation, you will be able to get some credentials like App ID, App Secret, and App Key. We will talk about their usage in the coming sections.

Nginx

In order to run a PHP application in Modulus, you need to have a web server configured to serve your application. We will use the following Nginx configuration:
We have completed the necessary environment settings to continue with the development. Let's go to the design part.

Project Design From Scratch

Model

If you have ever used an ORM framework before, you will be very familiar with this topic. In Laravel projects, domain models are placed in the app/ folder by default. In this application, we will perform CRUD operations on messages, and this means we need to create a Message model.
If you want to create a model, simply create a class that extends the Model class, which is an abstract class in the Laravel core package Illuminate\Database\Eloquent. Create a file called Message.php under the app/ folder, and put the following content inside the file:

Comments