easy-chat-pushjs

Push Notifications using websocket, support android, IOS and browser background notifications (FCM) and Slack notifications.

Usage no npm install needed!

<script type="module">
  import easyChatPushjs from 'https://cdn.skypack.dev/easy-chat-pushjs';
</script>

README

This package is still under development and made for paidtabs.com & ziggs.io

it's not supposed be available for public, so some parts might be unclear.

Your PR's are really appreciated

npm Installation

npm install --save easy-chat-pushjs

Server Installation

  • Install redis [required for onlyOneConnection feature] learn more
  • Install ZMQ [required to push notifications to the websocket server] learn more

Features

  • Serverless authentication, JWT
  • Push a message to slack channel
  • The ability to disable multiple connections [ see uniqueToken -> createUserToken Function ]
  • User can publish to specific room only.
  • Subscribe to multiple rooms e.g. gender,male rooms in case you want to send a message to just the male rather than all genders.
  • android, browser and IOS notifications directly from your app based on firebase cloud messaging.
  • Works remotely i.e you can have the websocket on a separate server and use it from any other nodejs app. zmq should listen to 0.0.0.0 instead of 127.0.0.1

Push notifications example

const {User} = require('easy-chat-pushjs');
const path = require('path');

const user = new User({
    version: 1.0, // used to invalidate the old tokens (must match the server version)
    jwtPrivateKey: path.dirname(__dirname) + '/private.key', // absoulte path to the "jwt private key", used to encrypt the token.
    slackURL: "https://hooks.slack.com/services/EXAMPLE/....",
});

create token, client/ frontend gonna use this to connect to the websocket.

const token = user.createUserToken(['gender', 'male'] , null, 'dasfasdasd2342', {username: 'admin'});

send a message to everyone in the room 'gender' (this message will be delivered to client's websocket);

const publish = user.pushNotification('gender', {test: true} , null);

send a message to a specific user based on token's userId.

const publish2 = user.pushNotification(null, {test: true} , 'dasfasdasd2342');

push a browser / android / ios notification based on user token. take a look on the following link if you want to know how to get browser token.

const firebase = user.pushFirebaseNotifications(['FCM_TOKEN'], 'test', 'You have received a new request' );

push slack notification, you need a webhook url to be set while configuring the server, for more information click here

const slack = user.pushSlackMessage('You have received a new request');

If you want to wait to get a response of the functions run the following

Promise.all([publish,publish2,firebase,slack,token]).then(res => {
    console.log(res);
})

Run WebSocket Server:

Make sure redis is up and running, in terminal type: redis-server create your server.js file (see examples websocket.js) create your jwt RS256 key pairs and set the path in the options object.

Create RS256 key pairs on Unix-like OS

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

Run your server.js by calling forever server.js

You may need to install forever (npm i forever -g)

optionally you can enable FCM and Slack API if you're planning to use them.

Nginx Setup Proxy

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream socketio {
    server 127.0.0.1:5511;
}

Server{

  location ^~ /websocket/ {
       
        proxy_pass http://socketio;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
        
        // if over loadbalancer or cloudflare 
        
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  
}  

More Examples

Take a look on the examples' directory, you'll find there:

  • How to start the websocket server. Check websocket.js
  • How to connect from frontend to the server and chat or receive push notifications. Check client.js
  • How to push notifications, push messages to slack and Android- IOS (FCM). Check push.js

Notes