<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>integration — Simple Engineering</title>
    <link>https://getsimple.works/tag:integration</link>
    <description></description>
    <pubDate>Sun, 15 Nov 2026 00:39:39 +0000</pubDate>
    <item>
      <title>Integration of redis in nodejs applications </title>
      <link>https://getsimple.works/integration-of-redis-in-nodejs-applications?pk_campaign=rss-feed</link>
      <description>The reactive aspect of nodejs applications is synonymous with the nodejs runtime itself. Even though the real-time aspect may be attribut...</description>
      <content:encoded><![CDATA[<p>The reactive aspect of <code>nodejs</code> applications is synonymous with the <code>nodejs</code> runtime itself. Even though the real-time aspect may be attributed to WebSocket implementation, the <strong><em>realtime reactive aspect</em></strong> of <code>nodejs</code> applications heavily rely on pub/sub mechanisms — most of the time backed by datastore engines like <code>redis</code>.  This article explores how to integrate <code>redis</code> datastore into a <code>nodejs</code> application.</p>

<p><strong><em>In this article we will talk about:</em></strong></p>
<ul><li><code>redis</code> support with and without <code>expressjs</code></li>
<li><code>redis</code> support with and without WebSocket push mechanism</li>
<li>Alternatives to <code>redis</code> in <code>nodejs</code> world and beyond</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>The following code sample showcase how <code>nodejs</code>/<code>redis</code> integration can be achieved. It demonstrates that it is still possible to share sessions via a middleware between <code>socket.io</code> and <code>expressjs</code>.</p>

<pre><code class="language-JavaScript">var app = express();
var server = Server(app);
var sio = require(&#34;socket.io&#34;)(server),
	redis = require(&#39;redis&#39;), 
	rhost = process.env.REDIS_HOST,
	rport = process.env.REDIS_PORT,
	pub = redis.createClient(rport, rhost), 
	sub = redis.createClient(rport, rhost);


function middleware(req, res, next){
 //session initialization thing
 next();
}

//socket.io/expressjs session sharing middleware
sio.use(function(socket, next){
 	middleware(socket.request, socket.request.res, next);
});

//express uses middleware for session management
app.use(middleware);
    
//somewhere
sio.sockets.on(&#34;connection&#34;, function(socket) {
 
 //socket.request.session 
 //Now it&#39;s available from `socket.io` sockets too! Win!
 socket.on(&#39;message&#39;, (event) =&gt; {
	 var payload = JSON.parse(event.payload || event),
	 	user = socket.handshake.user || false;
	 
	 //except when coming from pub  			
	 pub.publish(payload.conversation, payload)); 
 });

 //redis listener
 sub.on(&#39;message&#39;, function(channel, event) {
	var payload = JSON.parse(event.payload || event),
		user = socket.handshake.user || false;
	sio.sockets.in(payload.conversation).emit(&#39;message&#39;, payload);
 });
</code></pre>

<p><em><em>Example</em>: excerpt <a href="https://stackoverflow.com/a/25618636/132610">source: StackOverflow</a></em></p>

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

<p>When trying to figure out how to approach <code>redis</code> datastore integration into <code>nodejs</code> application for inter-process communication and real-time feature, the following points may be a challenge:</p>
<ul><li>How to decouple the WebSocket events from the <code>redis</code> specific (<code>pub</code>/<code>sub</code>) events. We should be able to decouple, but still provide an environment where interoperability is possible at any time.</li>
<li>How to make integration modular, testable, and overall friendly to the rest of the application ecosystem</li></ul>

<p>When testing this implementation, we should expect the additional challenge to emerge:</p>
<ul><li>The <code>redis</code> client instances (<code>pub</code>/<code>sub</code>) are created as soon as the library loads, and a <code>redis</code> server should be up and running by that time. The issue is when testing the application, there should be no server or any other system dependency hindering the application from being tested.<br/></li>
<li>getting rid of <code>redis</code> server with a drop-in-replacement, or <code>stubs/mocks</code>, is more of a dream than reality ~ hard but feasible.</li></ul>

<blockquote><p>There is additional information in mocking and stubbing <code>redis</code> data store in <a href="./how-to-mock-redis-datastore.md">“How to Mock <code>redis</code> datastore”</a> article.</p></blockquote>

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

<p>In this article, we revisited how to enhance <code>nodejs</code> application with <code>redis</code> based pub/sub mechanism, critical to having a reactive real-time experience. 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>Sharing session between <code>expressjs</code> and <code>socket.io</code> ~ <a href="https://stackoverflow.com/questions/25532692/how-to-share-sessions-with-socket-io-1-x-and-express-4-x">StackOverflow Question</a></li>
<li>examples using <code>redis</code> with <code>socket.io</code> ~ <a href="https://stackoverflow.com/questions/9267292/examples-in-using-redisstore-in-socket-io">StackOverflow Question</a></li>
<li>Using <code>redis</code> as <code>PubSub</code> over <code>socket.io</code> ~ <a href="https://stackoverflow.com/questions/32743754/using-redis-as-pubsub-over-socket-io">StackOVerflow Question</a></li>
<li><a href="https://github.com/socketio/socket.io-redis"><code>socket.io</code> – <code>redis</code></a></li>
<li><code>redis</code> <code>pubsub</code> library ~ <a href="https://github.com/murindwaz/redispubsub">Github Cloned Library</a></li>
<li>Building a Chat Server with node and <code>redis</code> – tests ~ <a href="https://matthewdaly.co.uk/blog/2014/12/31/building-a-chat-server-with-node-dot-js-and-redis/">Matthew Daly Blog</a></li>
<li>High Volume, low latency difficulties <code>nodejs</code>/<code>pubsub</code>/<code>redis</code> ~ <a href="https://stackoverflow.com/questions/10557826/node-js-socket-io-redis-pub-sub-high-volume-low-latency-difficulties">StackOverflow Question</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:redis" class="hashtag"><span>#</span><span class="p-category">redis</span></a></p>
]]></content:encoded>
      <guid>https://getsimple.works/integration-of-redis-in-nodejs-applications</guid>
      <pubDate>Thu, 17 Jun 2021 04:46:06 +0000</pubDate>
    </item>
    <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>