<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>websocket — Simple Engineering</title>
    <link>https://getsimple.works/tag:websocket</link>
    <description></description>
    <pubDate>Tue, 24 Nov 2026 14:27:32 +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>The reactive aspect of nodejs applications is synonymous with the nodejs runtime itself. However, the real-time magic is attributed to th...</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">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">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">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">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>