sametsahindogan / nats-message-channel

:pushpin: Implementation of NATS MQ & websocket protocol in NodeJS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nats-message-channel

GitHub license

Implementation of NATS MQ with websocket protocol.

Scenario

Let's say we want to send real-time notifications/messages to our users without using any 3rd party service like Socket IO or Pusher.

And of course we want to separate it from our backend server to scale it freely.

Our implementation scenario will be as follows;

  • Step 1: Of course, there should be an authentication process. When users first load the page, they send a message to our Websocket server with their own tokens.

  • Step 2: Our Websocket server send these credentials to the authentication channel (via req/reply method) on the NATS server.

  • Step 3: Our backend server has subscribed to the authentication channel. It performs the authentication process according to the incoming message.

  • Step 4: Once authentication is successful, our Websocket server marks this connection as authed and allows it to subscribe to the user-specific notification channel on the NATS server.

  • Step 5: When an event occurs (eg. order received), our backend server sends this notification to the user-specific notification channel on NATS server.

  • Step 6: Our websocket server receives the notification message through the NATS server and sends it to the user as real-time.

Our websocket server is written in NodeJS.

Suppose that our backend project is written in PHP. At that case our publisher and subscriber will be written in PHP.

Installation

First of all, you should install to NATS server.

Go to /websocket folder and run ;

npm install

Go to /api folder and run;

composer install

Usage

We need to run our Websocket server and Auth Subscriber.

  1. Go to /api folder and run;
node app.js
  1. For authentication, we run our authentication subscriber in our backend. Go to /api folder and run;
php authSubscriber.php
  1. Open /client/user1.html file on web browser then click the subscribe button.

Now you can send a notification to the user-specific notification channels that the user is subscribed.

For example;

  1. Go to /api and run this command ( first argument is user id, second is message );
php notificationPublisher.php 1 hello!

A screenshot of the processes in the background. You can follow the life cycle from the descriptions on terminal;

Summary

You can adapt it to your own project, inspired by the architecture in this project.

License

MIT © Samet Sahindogan

About

:pushpin: Implementation of NATS MQ & websocket protocol in NodeJS.

License:MIT License


Languages

Language:JavaScript 60.8%Language:HTML 24.5%Language:PHP 14.8%