<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>websocket &amp;mdash; Simple Engineering</title>
    <link>https://getsimple.works/tag:websocket</link>
    <description></description>
    <pubDate>Mon, 20 Apr 2026 18:16:54 +0000</pubDate>
    <item>
      <title>Integration of WebSocket in nodejs application</title>
      <link>https://getsimple.works/integration-of-websocket-in-nodejs-application?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[The reactive aspect of nodejs applications is synonymous with the nodejs runtime itself. However, the real-time magic is attributed to the WebSocket addition. This article introduces how to integrate WebSocket support within an existing nodejs application.&#xA;&#xA;In this article we will talk about: &#xA;&#xA;WebSocket support with or without socket.io&#xA;WebSocket support with or without expressjs&#xA;Modularizations of WebSocket &#xA;&#xA;  Even though this blog post was designed to offer complementary materials to those who bought my Testing nodejs Applications book, the content can help any software developer to tuneup working environment. You use this link to buy the book.  Testing nodejs Applications Book Cover&#xA;&#xA;Show me the code&#xA;&#xA;Express routes use socket.io instance to deliver messages of a socket/socket.io enabled application looks like following: &#xA;&#xA;//module/socket.js - server or express app instance &#xA;module.exports = function(server){&#xA;  var io = socket();&#xA;  io = io.listen(server);&#xA;  io.on(&#39;connect&#39;, fn); &#xA;  io.on(&#39;disconnect&#39;,fn);&#xA;};&#xA;//OR&#xA;//module/socket.js &#xA;var io = require(&#39;socket.io&#39;);&#xA;module.exports = function(server){&#xA;  //server will be provided by the calling application&#xA;  //server = require(&#39;http&#39;).createServer(app);&#xA;  io = io.listen(server);&#xA;  return io;&#xA;};&#xA;&#xA;//module/routes.js - has all routes initializations&#xA;var route = require(&#39;express&#39;).Router();&#xA;module.exports = function(){&#xA;    route.all(&#39;&#39;,function(req, res, next){ &#xA;    &#x9;res.send(); &#xA;    &#x9;next();&#xA; });&#xA;};&#xA;&#xA;//in server.js &#xA;var app = require(&#39;express&#39;).express(),&#xA;  server = require(&#39;http&#39;).createServer(app),&#xA;  sio = require(&#39;module/socket.js&#39;)(server);&#xA;&#xA;//@link http://stackoverflow.com/a/25618636/132610&#xA;//Sharing session data between SocketIO and Express &#xA;sio.use(function(socket, next) {&#xA;    sessionMiddleware(socket.request, socket.request.res, next);&#xA;});&#xA;&#xA;//application app.js|server.js initialization, etc. &#xA;require(&#39;module/routes&#39;)(server); ;               &#xA;&#xA;What can possibly go wrong?&#xA;&#xA;When working in this kind of environment, we will find these two points to be of interest, if not challenging: &#xA;&#xA;For socket.io application to use same expressjs server instance, or sharing route instance with socket.io server&#xA;Sharing session between socket.io and expressjs application&#xA;&#xA;Conclusion &#xA;&#xA;In this article, we revisited how to add real-time experience to a nodejs application. The use of WebSocket and Pub/Sub powered by a key/value data store was especially the main focus of this article. There are additional complimentary materials in the &#34;Testing nodejs applications&#34; book.  &#xA;&#xA;References &#xA;&#xA;Testing nodejs Applications book&#xA;socket.io within concurrent processes ~ Tolleiv Nietsch Blog&#xA;nodejs, expressjs and socket.io application ~ DevelopIQ Blog&#xA;Modularize Your Chat App(or How to Write a nodejs Express App in More Than One File) ~ Philosophie Is Thinking Medium&#xA;Bacon.js + nodejs + MongoDB: Functional Reactive Programming on the Server ~ Carbon Five Blog&#xA;Modularizing socket.io with express 4 ~ StackOverflow Answer&#xA;&#xA;#snippets #nodejs #integration #WebSocket]]&gt;</description>
      <content:encoded><![CDATA[<p>The reactive aspect of <code>nodejs</code> applications is synonymous with the <code>nodejs</code> runtime itself. However, the real-time magic is attributed to the WebSocket addition. This article introduces how to integrate WebSocket support within an existing <code>nodejs</code> application.</p>

<p><strong><em>In this article we will talk about:</em></strong></p>
<ul><li>WebSocket support with or without <code>socket.io</code></li>
<li>WebSocket support with or without <code>expressjs</code></li>
<li>Modularizations of WebSocket</li></ul>

<blockquote><p>Even though this blog post was designed to offer complementary materials to those who bought my <strong><em><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></em></strong>, the content can help any software developer to tuneup working environment. <strong><em><a href="https://bit.ly/2ZFJytb">You use this link to buy the book</a></em></strong>.  <a href="https://bit.ly/2ZFJytb"><img src="https://snap.as/a/42OS2vs.png" alt="Testing nodejs Applications Book Cover"/></a></p></blockquote>

<h2 id="show-me-the-code" id="show-me-the-code">Show me the code</h2>

<p>Express routes use <code>socket.io</code> instance to deliver messages of a <code>socket/socket.io</code> enabled application looks like following:</p>

<pre><code class="language-JavaScript">//module/socket.js - server or express app instance 
module.exports = function(server){
  var io = socket();
  io = io.listen(server);
  io.on(&#39;connect&#39;, fn); 
  io.on(&#39;disconnect&#39;,fn);
};
//OR
//module/socket.js 
var io = require(&#39;socket.io&#39;);
module.exports = function(server){
  //server will be provided by the calling application
  //server = require(&#39;http&#39;).createServer(app);
  io = io.listen(server);
  return io;
};

//module/routes.js - has all routes initializations
var route = require(&#39;express&#39;).Router();
module.exports = function(){
    route.all(&#39;&#39;,function(req, res, next){ 
    	res.send(); 
    	next();
 });
};

//in server.js 
var app = require(&#39;express&#39;).express(),
  server = require(&#39;http&#39;).createServer(app),
  sio = require(&#39;module/socket.js&#39;)(server);

//@link http://stackoverflow.com/a/25618636/132610
//Sharing session data between SocketIO and Express 
sio.use(function(socket, next) {
    sessionMiddleware(socket.request, socket.request.res, next);
});

//application app.js|server.js initialization, etc. 
require(&#39;module/routes&#39;)(server); ;               
</code></pre>

<h2 id="what-can-possibly-go-wrong" id="what-can-possibly-go-wrong">What can possibly go wrong?</h2>

<p>When working in this kind of environment, we will find these two points to be of interest, if not challenging:</p>
<ul><li>For <code>socket.io</code> application to use same <code>expressjs</code> server instance, or sharing route instance with <code>socket.io</code> server</li>
<li>Sharing session between <code>socket.io</code> and <code>expressjs</code> application</li></ul>

<h2 id="conclusion" id="conclusion">Conclusion</h2>

<p>In this article, we revisited how to add real-time experience to a <code>nodejs</code> application. The use of WebSocket and Pub/Sub powered by a key/value data store was especially the main focus of this article. There are additional complimentary materials in the <strong>“Testing <code>nodejs</code> applications”</strong> book.</p>

<h2 id="references" id="references">References</h2>
<ul><li><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></li>
<li><code>socket.io</code> within concurrent processes ~ <a href="https://blog.tolleiv.de/2014/05/socket-dot-io-within-concurrent-processes/">Tolleiv Nietsch Blog</a></li>
<li><code>nodejs</code>, <code>expressjs</code> and <code>socket.io</code> application ~ <a href="https://developeriq.in/articles/2015/feb/03/nodeexpressjs-and-socketio-application/">DevelopIQ Blog</a></li>
<li>Modularize Your Chat App(or How to Write a <code>nodejs</code> Express App in More Than One File) ~ <a href="https://medium.com/philosophie-is-thinking/modularize-your-chat-app-or-how-to-write-a-node-js-express-app-in-more-than-one-file-bfae2d6b69df#.hfb4r6z3i">Philosophie Is Thinking Medium</a></li>
<li><code>Bacon.js</code> + <code>nodejs</code> + <code>MongoDB</code>: Functional Reactive Programming on the Server ~ <a href="https://blog.carbonfive.com/2014/09/23/bacon-js-node-js-mongodb-functional-reactive-programming-on-the-server/">Carbon Five Blog</a></li>
<li>Modularizing <code>socket.io</code> with express 4 ~ <a href="https://stackoverflow.com/a/29697470/132610">StackOverflow Answer</a></li></ul>

<p><a href="https://getsimple.works/tag:snippets" class="hashtag"><span>#</span><span class="p-category">snippets</span></a> <a href="https://getsimple.works/tag:nodejs" class="hashtag"><span>#</span><span class="p-category">nodejs</span></a> <a href="https://getsimple.works/tag:integration" class="hashtag"><span>#</span><span class="p-category">integration</span></a> <a href="https://getsimple.works/tag:WebSocket" class="hashtag"><span>#</span><span class="p-category">WebSocket</span></a></p>
]]></content:encoded>
      <guid>https://getsimple.works/integration-of-websocket-in-nodejs-application</guid>
      <pubDate>Thu, 17 Jun 2021 04:43:53 +0000</pubDate>
    </item>
  </channel>
</rss>