Node.js Is Genius with WebSockets

by | Feb 6, 2018

Originally published: MC Press Online on 5/29/15

“Wow, that was significantly simpler than I thought it would be,” said no web programmer ever.

I am often in the camp of wishing things were simpler with web development, specifically the link between the browser and server, so that I don’t have to think as much about the underlying technology and can focus more on meeting the business need. Many technologies have made strides in this area over the years with concepts of convention over configuration (thank you, Ruby on Rails). Today, I am tooting the horn of JavaScript and Node.js in the implementation of HTML5 WebSockets.

Wikipedia describes HTML5 WebSockets as “a protocol providing full-duplex communication channels over a single TCP connection”.

HTML5 is just a specification, not an implementation. It’s every technology stack’s responsibility to implement said spec and not only make everything adhere to the spec but also put some amount of focus on making usage easy for the web developer. In the case of Node.js, we have a Node Package Module (NPM) named that implements HTML5 WebSockets with excellence to the point of being surprisingly simple and easy to use.

Our goal in this article is to use to create a chat application using HTML5 WebSockets, as shown in Figure 1. The concept is that two browser clients will obtain a WebSocket connection to the server so chat messages entered in one browser are delivered from the server to the client in another browser without the client polling the server. It is important to note the lack of polling in that last sentence. That’s how things were done in the past. Now, the server can initiate communication down to the client. Very cool!

Here's a chat application.

Here’s a chat application.

Before we move on to writing the chat application, it would be good to [[review how to set up and use your Node.js environment(*Note: post “Node.js is Here. What to Do?”)]].

Open up an IBM i shell prompt and create a new directory named nodejs_websocket and cd (change directory) into it, as shown below.

Next, we want to initialize the nodejs_websocket folder as a Node.js application by running the following command. This will prompt you for a few questions and in the end create a package.json file that is used to store information about the application. More on package.json in a bit.

Now, install expressjs using the following command. Specifying --save will cause this to be placed as an application dependency in the package.json file.

Now, install with command “npm install --save“. At this point, your package.json file should look similar to the following. Notice the two entries in dependencies have both modules we just installed.

Next, we will create the two files necessary for this application using the touch command, namely app.js and index.html, as shown below:

Below, we have the entirety of the app.js code, which resides entirely on the server. The first three lines are bringing in outside functionality, much like a /COPY in RPG. Line 5 is waiting for requests to the root of the domain and, once invoked, will send the index.html file down to the browser.

Line 9 is listening for the connection event and is where WebSocket type functionality is introduced. The connection event occurs in, and is initiated from, the client (or rather from within index.html). Below is the entirety of the index.html file. Lines 1 through 15 aren’t necessary to discuss. The important part starts when we first include the client-side library from its Content Delivery Network (CDN). I say client-side library to call out the fact that while is one tool, it has two parts: one for the client-side and the other for the server-side. Line 23 is where the WebSocket connection is initiated to the server. Once the connection is made, both the server and client go into a wait state where either can initiate communication to the other. There is one exception to that last sentence. If the client doesn’t support WebSockets, then the polling of client-to-server approach will be employed.

Line 24 is jQuery listening for the form to be submitted. This is not part of WebSockets, but it’s important for ease of client-side programming. Line 25 is important. This is where the app is “emitting” a message to the server. Looking back at app.js, there is line 13 “listening” for “chat message” events. When it receives one, it then emits messages to all other browser clients that are listening for the same “chat message” event. The emitting of a new message from server to clients is on line 14. does an excellent job of hiding complexity if you ask me. Going back to index.html line 29, we can see the “chat message” being listened for. When a message is received, jQuery is used to obtain reference to #messages, adding the entry to the bottom. At this point, the full loop of communication is complete and all users see the new chat message.

I purposely didn’t go into details of the WebSocket technology in this article. Sometimes we have the luxury of not needing an intimate understanding of the technology going on under the covers. I believe this is the case with Hopefully, this gives you a good idea of one way WebSockets can be used and maybe even sparks some ideas of your own.

Stay tuned for an article introducing Git on IBM i!


  • Call web APIs from RPG
  • Process XML and JSON
  • Offer web services

Litmis Spaces

Affordable IBM i cloud hosting

CyberSource Toolkit for i

  • Process credit cards from native RPG
  • Integrate CyberSource payment services

Follow Us

Need help implementing Node.js and Websockets? We can help. Contact us below.

  • This field is for validation purposes and should be left unchanged.

Get Social

Share to your favorite social platform