Building a socket api with node js, express, websocket, mongoDB and firecamp: (Login and registration)

This will be a detailed three part series to get you up and ready to build a cool real time back-end monolithic service.

Table of content

  1. Login and registration
  2. User profile edit with authentication with JWT
  3. Data validation

In this part, we will create login and registration socket application using Node.js. We will use Express.js to create the server and MongoDB to store our data.

Requirements

Before we get started, you need a few tools close to help in your development process.

Getting started

Let us get up to gear. initialize your node application. This creates a file called package.json inside root directory. The file is responsible for managing the dependencies used by the application.

$ npm init — y

packages: bcrypt, body, cors, dotenv, express, jsonwebtoken, mongodb, mongoose, nodemon, ws

Setting up folder structure:

Create a server.js file in the project root directory of your project and setup the express server to listen to port 8080 (the default port for WebSockets) with a mongoDB connection by passing connection URI inside the connect method.

Setting up actions

These are pretty straight forward and will be used in our case statements

  • user.action.js

Back in the server.js file, let us initialize a web-socket connection and listen to incoming messages. Web-socket listens to events with the on method.

Okay. we are half way there:

Let us create our simple user model file for our mongo db and import it in our user controller file.

Create our User.controller file: Remember the actions we created earlier? Now we can use them. By now, if you have prior exmongoperience writing code in express, things have started looking familiar. The only difference is instead of regular http res.send, we use socket.send to the client and all data or objects must be stringify()

Wondering what preloadUsers is? I use it to preload data the first time the client connects to the socket server. Though in this case we are not going to use it.

The next step is to import the controller and preloader to the server:

Final server.js file:

With this, we are ready to give the service a test run with our downloaded firecamp application: The object that will be used to send requests:

  • signin
  • login

Explaining functions use within the application

ws.on(‘connection’, async(socket) => { } :

Starts a socket connection and keeps track of all connected clients.

socket.on(‘message’, async(message) => { }

Here we listen to all messages/data from client to server anv server to client. As usual, the data which is a json object is converted to a string.

socket.on(‘close’, () => { }

Keeps track of all disconnected clients.

async function userController(ws, socket, message) { }

This is a controller function that accepts three parameters. the protocal ws: unlike in a rest api where we use http:// or https:// in websockets we use ws:// or wss://. message: this is where all data streaming happens.

async function preloadUsers(socket) { }

:optional:

Next: We will create a User profile controller edit with jwt authentication.

Written by

I am an experienced back-end software engineer adept in bringing forth expertise in design, installation, testing and maintenance of software systems.

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