Chat-app Creation Using Vue.js, Nuxt.js, Node.js, Socket.IO, Vue-Socket.IO, Vuetify.js Technologies

Hello everybody. Recently I’ve got a desire to master the Socket.IO library and create a chat application, to strengthen theoretical knowledge with practice, so to say.

I actively use the technological stack which is implemented in the application in my work on commercial projects, but for Socket.IO.

It’s easy to inject the above-mentioned library into the already working project, but today I am going to speak about creating an app from scratch.

Let’s proceed, I do not like long forewords myself.

Setting up and installation of the Nuxt.js generic template.

  • You need to have Node.js installed, otherwise — install it.
  • If your NPM version is under 5.2 — install npx globally, using the rights of admin $sudo npm install -g npx.
  • After that create a project with the help of the following command:

$npx create-nuxt-app <project-name>

(here you will find more information on project creation).

  • Then a project configuration menu will appear (I used my project details):
  1. Project name — “Nuxt-chat-app”
  2. Description — “Simple chat with Nuxt.js”
  3. Author’s name — “Petr Borul”
  4. Package manager — “NPM”
  5. UI framework — “Vuetify.js”
  6. Server Framework — “Express”
  7. Nuxt.js modules — “PWA”
  8. Linting tools — “ESLint”
  9. Test framework — “none”
  10. Rendering mode — “Universal”

Let’s install SOCKET.IO: $npm install socket.io — save

I also used a wrapper for SOCKET.IO — Vue.SOCKET.IO.

In this library, you can call the websocket events and subscribe to them through the Vuex store, but for the hands-on review of the library, it is too implicit. That’s why I implemented the logic at the component level (here is a description of the two possible ways of its implementation).

$npm install vue-socket.io — save

The full information on Nuxt.js folders’ structure you can find here.

The main points:

  • The folder pages contains views and routes. The framework reads all .vue files inside the folder and creates a router for the application.
  • The folder plugins contains JavaScript-plugins, which are run before the root application Vue.js creation (here our plugin socket.io will be resided).
  • The folder middleware contains the intermediate processing functions (the named ones are created in this folder, and if you want to specify the anonymous ones — you can declare them inside the component).
  • The file nuxt.config.js contains Nuxt.js user configuration.
  • The folder store contains the files of Vuex container. After index.js file creation in this folder, the container is activated automatically.

So, we have dealt with the main notions, let’s proceed to the app development itself.

The folder contains the file index.js — we’ll change it a bit and take the server configuration to a separate file app.js.

We’ll add server configuration to index.js:

Then we’ll order Node.js to listen to the server configured:

Further on we create the file socket.client.js and add it to the folder plugins, we indicated the file extension ‘client’, because we need it only on the client-side (Here you can find all the info as to the plugin’s adjustments).

socket.client.js

Now we’ll register it in the nuxt.config.js file:

From this point on you can refer to it in any component, using only the name of the file this.$socket.emit().

In the app.js file we’ll create two models of working with the data:

message.js

users.js

We have finished with the server at this point and now we’ll proceed to the client side.

In the folder store we’ll create index.js file and add the store index.js:

Further on we’ll add a layout to the file index.js in folder layouts (I use UI library Vuetify.js because of the Material Design, which I like very much).

index.js

When the submit () method is called, the form is validated, and in case of success, we send the event to the server this.$socket.emit().

We send a String with the name of the event to the server, and a callback function, after the fulfillment of which we get an ID and assign it to the user’s object, then we write it down to the state and send it to the chat page.

Let’s describe the event processing on the server:

  1. The event “connection” is called when the user gets the connection with the server.
  2. Then we subscribe to the event received from the client with the help of socket.on().
  3. This function accepts the String <event-name> and the callback function.
  4. We add a new user to the users’ list and assign ID the corresponding ID socket for connection.
  5. We pass the ID on to the client’s side.

Now we’ll create the layout of the default.vue file in the layouts folder, it’s set by default for all the components in the folder pages if the layout is not indicated (here you’ll find the detailed information).

default.vue

The tag <nuxt /> is responsible for views on various routes (read more detailed info about the views in Nuxt.js here).

The object sockets is responsible for the processing of the events, which are called on the server side.

Let’s add subscription for 2 events “updateUsers” and “newMessage”.

Then we’ll add the method exit(), which will be called with an exit button click and in which we will send the event “leftChat” to the server. Then the user will be redirected to the registration form from the query on the route for message display in the snackbar.

Let’s process this event on the server:

app.js

Now we’ll create a file auth.js in the middleware folder and add an intermediate processing function to the component so that only an authorized user could get on the chat page.

auth.js (open and close on a click on the file name):

Also, with the initialization of the component we send the event “joinRoom” to the server and send the user data as a payload into the feedback function.

Let’s process this event on the server:

app.js

  • We add the user to the room, which he indicated during the authorization;
  • then we call the event “updateUsers” for all the users of the room;
  • and call the event “newMessage” only for the user, who has called the event “joinRoom”;
  • We call the event “newMessage” for all the users, except for the current user ( notify the other users about the new user, who joined).

Further on we’ll add the chat layout.

chat.vue

I have omitted the section with styles, for you to concentrate on the logic.

The component, which is responsible for message rendering is Message.vue

The styles are adjusted in the same way as the previous component.

The component for message realization and sending is ChatForm.vue

When a form is verified — we send an event “createMessage” to the server, send the message text and the user ID, after the feedback function, we clear the field.

Now we’ll process this event on the server:

app.js

We’ll add the subscription in case the connection fails and it will be possible to add the reconnect possibility later on.

app.js

By now it’s the final part of the app.

You can launch the local server with the help of the command:

$npm run dev

Preview

Github

As you can see the Socket.IO library is very simple and easy to use.

After the development had been finished I had a desire to deploy the app and share the demo version of it with you. I spent some time on the search of the suitable free service, which supports WebSockets. Finally, I chose Heroku. Nuxt.js manuals have a detailed guide about how to deploy an app onto this service.

Demo

Originally published in Stfalcon.com

Read the continuation of the article with new features from Petr Borul. Thanks for your attention!

--

--

--

Ukrainian IT company designing best solutions for Europe and USA. We share most valuable IT experiences. Our goal is to create useful and convenient software

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ProcessWire & Vue.js — a Lovestory

11 Ways to Improve Your Technical Resume

How to route React app in a subdirectory

Firebase Functions: Send Email using Google OAuth2

Learn How to Implement Agora in Web 2022

Learn How to Implement Agora in Web

Easy to Learn React Hooks

Fetching Data in React using Hooks

Unit Tests VS Integration Tests

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Stfalcon.com

Stfalcon.com

Ukrainian IT company designing best solutions for Europe and USA. We share most valuable IT experiences. Our goal is to create useful and convenient software

More from Medium

Uploading a CSV File to Postgres using Vue and Laravel

Nuxt + Storybook with Sass

Todo App using VueJS 3 Composition API and Tailwind CSS

Actual App Screenshot Showcase

Deployment/Hosting of Vue.js app using Firebase