WebSocket Using Node Socket.io Notes

Last Edited: 2022-11-13 19:52:49

My previous experience with websockets involved a server running on Ruby on a linux server, and Chrome setting up the WebSocket object allowing two way interaction between client and server.


ruby is version 2 and capable of running the websockets server (ruby 2.0.0p648 (2015-12-16) [x86_64-linux]), and my previous experience also showed that you can connect to MySQL via ruby. However after some research, it may not be the best option.


PHP is able to run a websocket server, but I found that it is not a good option because it uses a lot of server resources. Since that was the origin of the issues, I did not look into this solution any further. It also used $ php -S as the server which is suggested in the PHP manual to not use in a production environment.


Python was my next step as I have more experience programming in Python than I do in ruby, however, Python’s websockets library uses python 3. There are a most likely lot of native linux programs that use the installed python version (2.7.18) that makes me not want to use this option. The jump to Python3 is not backwards compatible. The solution to this is to have both Python2 and Python3 in the same box. Every time I’ve done this in the past has been met with many problems, some of the with the OS itself.

On the off-chance Python would be an option, I upgraded my linux server to Ubuntu 20.04 which comes standard with Python3, I created a test script there. Using Python would mean building the actual websocket server from scratch. This presents a lot of work, but you do have more control. You also have more problems. Since my server and fullbay connects with HTTPS, the secure websocket protocol WSS would need to be used. Once I changed the host protocol to WSS, I could not get the websocket to connect back to the server.

Node.js && Socket.IO

In my reading, I found that Node.js has a library called Socket.IO which has made its way to the top of the list for websockets integration over the past few years. I put together a test script and was up and running very quickly. Since this is a library you do not have as much control over the server itself, however it seems to take everything into account anyhow.

Node.js is built on the principals of JavaScript. This was my first interaction with Node.js but with many years of JavaScript it was very easy to pick up how the structure and syntax works for the language. It is also easier because the language for the client-side is also JavaScript.

Node is not currently on the fullbay server, but should be easily added using

$ yum install nodejs
$ yum install npm

Once those are installed, it needs at least these packages added:

$ npm install express@4.17.1
$ npm install audit
$ npm install socket.io
$ npm install mysql

On my server, I put together a quick connection that closely mimics what we want to do.  I started with a chat tutorial.  The Node.js server can be run from the command line where it will output a log for development.  The same can be run into a background process which the std data can be redirected to a log file somewhere, probably something like /var/log/node.log.

When you start the node process, you utilize a separate port.  In the tutorial I was looking at you server the HTML page with the websocket connection through that port.  In my example, I fire up the node.js socket server on port 3000 and wait for connections:

When that connection is made, I use the chat window from the tutorial to send whatever is entered in.  For testing purposes, I just use my name.  In a more practical fullbay situation, it would be programmatically sent as $_SESSION['office']->EntityEmployee->getEntityEmployeeId().

When that is sent, it immediately pushes back a message that says “Hello Freddy” to show the transmission is happening in both directions correctly.

On the server side, in that same process, I save the name and the socket ID.  In this instance, since I have the socket ID saved, when a notification comes in, I can find that person, or entityEmployeeId and send only them a notification (I also have the ability to broadcast a message to any open socket, but that is out of scope for notifications).

Since fullbay will have multiple tabs open per user at most times, we can still facilitate saving the tab ID in JavaScript localStorage, and save the associated tab ID in the database to assure we send popup notifications to the active tab at the time the notification is created/retrieved.  We can also update all of the particular user’s notification counts by each active socket ID.

For instance, with two windows open, Socket.IO sees that as two separate users, but by sending my name back, which again would be $_SESSION['office']->EntityEmployee->getEntityEmployeeId().  Now you have the reference to send the notification count to both windows without the server being polled through apache.