This chat session allows registered and authenticated users to engage in a one-on-one conversation. This application will be built using Laravel and Vue.js. You can download the code for this project here on GitHub.
Basic knowledge of building web applications with Laravel will help you get the best out of this tutorial. Also, ensure that you have:
If you are yet to install the Laravel installer on your computer, run the following command to install it using composer:
composer global require laravel/installer
Once the installation process is complete, you will be able to use the laravel command for installing a new Laravel project. Proceed by using the command below to create a new Laravel project with some extra functionalities out of the box:
laravel new laravel-chat-app --auth
The preceding command will create a fresh Laravel installation in a directory named laravel-chat-app and place it in your development folder or where you ran the command from. Noticed the flag --auth? That was included to help create our application with all of the authentication scaffolding, which consist of the User model and migration file, Authentication controller, blade views for registration and login, main application layout file, and a default structure for Vue.js component and so on.
If you haven’t created a CometChat account yet, click here to set up a free CometChat Pro account. Once you are done, create a new CometChat app from your dashboard by clicking on the Add New App button:
Enter the desired name for your application, select a region, technology, and a use case. Once you are done, click on the Add App button to complete the process.
This will automatically spin up a new application for you on CometChat. Depending on the technology selected from the previous step, CometChat will make provision for a quick start guide to facilitate integrating CometChat inside your app or website with ease. You can check it out and go for your preferred option.
CometChat is by no means a user management solution. While it can maintain the session of the logged in user within the SDK, there is a limit to the authentication flow that can be handled by CometChat for your app. It is expected that authentication is managed for the users of your application before such a user is created or authenticated on CometChat.
To achieve this, we will make use of the CometChat Create user API to create a new user on CometChat once a user signs up in our application. And to log the user in, we will use the generated Auth Token specified for such user during the registration process. Consider this other method as the advanced authentication procedure for users on CometChat. More about this later in the tutorial.
If you open the project in your preferred code editor, you will noticed that a User model and its corresponding migration file has been generated already. We will need to make this conform to the structure of our application. Start by opening the migration file and replace its content with the following:
Here, we modified the content of this file to create fields for username, password and authToken in the database.
Next, open the User model in app/User.php file and modify the $fillable property to reflect this:
Next, with the User Model and migration file properly configured, go ahead and create a database for your application and update the .env file with its details:
Ensure that you replace the YOUR_DB_NAME, YOUR_DB_USERNAME and YOUR_DB_PASSWORD placeholders with your credentials.
Next, issue the following command to run the migration file and create the appropriate fields in your database:
php artisan migrate
After running the preceding command, we are now set to start implementing the authentication logic for this application.
In this section, we will modify the default authentication logic within Laravel to fit in into our use case. One of the reasons for the process is to return JSON response for the authentication controllers as this will be required to communicate with the frontend application that will be handled by Vue.js.
Start with the RegisterController found in app/Http/Controllers/Auth/RegisterController.php file and replace its content with the following code:
In the file above, we specified the required fields that should be validated during the registration process. And once the user was registered successfully, we returned a JSON response indicating the user’s details.
Next, open the app/Http/Controllers/Auth/LoginController.php file and use the following code for it:
We modified the logic here to ensure that users can log in by using their username instead of email. Also, once a user is logged in, we returned a User object as a JSON response.
The last function that needs to be created for this application is the updateToken() method. This method will carry out the functionality of persisting the generated token to uniquely identify a user on CometChat into the database. Open the RegisterController.php file and include the method below immediately after the register() function:
For the backend, update the route file in routes/web.php by including the following:
npm install @cometchat-pro/chat --save
Once the installation is complete, navigate back into the Laravel application and add the following variables to the .env file:
Replace the placeholders with the appropriate details obtained from your CometChat dashboard. Please be sure to use the value of your REST API Key instead of the Auth Key:
To initialize CometChat, open the resources/js/app.js file and use the following code for it:
In this section, we will create login, register and a chat view for users. To begin, create a folder named views within resources/js folder and create the following files within the newly created folder:
Lastly, we will leverage on the UI Kit created by CometChat team to set up our chat view. This comes with the benefit of an appealing user interface and a proper structure for chat applications. To use the kit, clone or download it from this repository using the following command:
git clone https://github.com/cometchat-pro/vue-chat-ui-kit.git
Once you are done, open it with a code editor or any file explorer and copy the cometchat-components folder and paste it in resources/js folder. We will reference some components from the file as required.
Open the Register.vue and paste the following content in it:
This <template></template> section of the file contains the HTML form and the input fields for username and password.
Next, paste the following code immediately after the template section:
As mentioned, the authentication flow for this application is to register users within it and also create such user immediately on CometChat using the Create User REST API. Here, we created the registerAppUser() to register users within our application. And once that is successful, we used the createUserOnCometChat() method to create the user on CometChat with the same unique username. We then proceeded to create an AuthToken for such user. This token will be used for authentication before such a user can be allowed to participate in a chat session.
Lastly, add the method below to save the generated token in the database for the user:
Open the Login.vue file and paste the code below in it:
We created the input fields for username and password. Next paste the following <script></script> that contains the logic to login a user:
Once a user can log in successfully into our application, we will retrieve the token generated earlier and use it to authenticate such user on CometChat.
To create a structure for the chat view, start by pasting the following code in resources/views/Chat.vue:
Here, we created a structure that brings together different UI components to represent and build a chat view of our application. Each of the components referenced here is from the UI kit downloaded earlier.
Next, update the <script></script> section with:
First, we imported CometChat SDK and three different components from the cometchat-components folder:
Next, we used the getLoggedinUser() method from the CometChat() class to retrieve the details of the currently logged in user.
So far, we have built the user interface into smaller Vue.js components and before we can make use of them within the appropriate Blade templates, register them as Vue.js components, by adding them to resources/js/app.js as shown here:
Here, we will make use of all the components created so far within each blade file from Laravel.
Start with the login.blade.php file in resources/views/auth folder and paste the following content in it:
Use the content below for resources/views/auth/register.blade.php:
Finally, render the chat component within resources/views/home.blade.php
Change the content rendered on the homepage by replacing the contents of resources/views/welcome.blade.php file with the following:
Replace the content of resources/sass/app.scss file with:
To update the Layout file. Replace the content of Layout.blade with the following:
Open different browser and run the command below to start the Laravel application:
php artisan serve
Next, to compile and start the frontend application use the following command:
npm run watch
Now, navigate to http://localhost:8000, to view the application in your browser:
Go ahead and register as a new user. You can create two separate accounts for testing purposes:
Log in, select a user and start a chat
Now, login and select a user to chat with. You will have a page similar to the one depicted by this image
Irrespective of the size of your application, you can easily implement a chat feature with CometChat SDK. As seen in this tutorial, we started with a non functional Laravel application, gradually built authentication, programmatically created a user and completed the application by including a chat feature. The complete source code can be found here on GitHub. Please feel free to clone it and improve on its existing features.