title: Socket.IO - Redis: SUBSCRIBE can't be processed. The connection is already closed
body: <p>I have a simple <code>Redis - Socket.IO</code> chat application which throws the following (redis)exception when a user hits F5 or tries to refresh the page:</p>
  
  <blockquote>
    <p>SUBSCRIBE can't be processed. The connection is already closed.</p>
  </blockquote>
  
  <p>When a user refreshes the page, the socket reconnects, and my sub <code>RedisClient</code> object throws this exception. This event on its turn causes my server.js to crash too. When commenting out all my sub and pub objects in the <code>io.sockets.on</code> connection, the socket reconnects without any exception, and the server.js keeps on running.</p>
  
  <p>My server.js</p>
  
  <pre><code>var socket = require('socket.io');
  var express = require('express');
  var http = require('http');
  var redis = require('redis');
  
  var app = express();
  var server = http.createServer(app);
  var io = socket.listen(server);
  
  var pub = redis.createClient();
  var sub = redis.createClient();
  
  app.use(express.static(__dirname));
  
  io.sockets.on('connection', function (client) {
  
      sub.subscribe("chatbox");
  
      sub.on("message", function (channel, message) {
          console.log("Message received, publishing...");
          client.send(message);
      });
  
      client.on("message", function (msg) {
          console.log(msg);
          pub.publish("chatbox", msg.message);
      });
  
      client.on('disconnect', function () {
          sub.quit();
          pub.publish("chatbox", "User: " + client.id + "  is disconnected");
      });
  
      client.on("error", function (err) {
          console.log("Error " + err);
      });
  });
  
  server.listen(8888);
  </code></pre>
  
  <p>The (simplified) html part:</p>
  
  <pre><code>$(document).ready(function() {
  
          var username ='Someone';
          var socket = new io.connect('http://localhost:8888');           
          var msg = {type:'setUsername',user:username};
  
          socket.json.send(msg);
  
          socket.on('connect', function() {
              console.log("Connected: " + Date());
          });
  
          $(window).on('beforeunload', function(){
              socket.close();
          })
  
          //...
  </code></pre>
  
  <p>The node.js stacktrace:</p>
  
  <pre><code>AbortError
  at handle_offline_command (c:\xampp\htdocs\chatbox\node_modules\redis\index.js:839:15)
  at RedisClient.internal_send_command (c:\xampp\htdocs\chatbox\node_modules\redis\index.js:873:9)
  at RedisClient.subscribe (c:\xampp\htdocs\chatbox\node_modules\redis\lib\individualCommands.js:419:17)
  at Namespace.&lt;anonymous&gt; (c:\xampp\htdocs\chatbox\nodeServer4.js:17:6)
  at emitOne (events.js:96:13)
  at Namespace.emit (events.js:188:7)
  at Namespace.emit (c:\xampp\htdocs\chatbox\node_modules\socket.io\lib\namespace.js:209:10)
  at c:\xampp\htdocs\chatbox\node_modules\socket.io\lib\namespace.js:177:14
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
  </code></pre>
  
  <p>It seems that the RedisClient objects cannot handle these socket disconnects. Adding a try catch around any of the sub, pub objects (like i.e. <code>try { sub.subscribe("chatbox"); } catch { }</code>) did not help either.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Adding the following lines keeps the server.js alive, but the exception is still thrown, not being able to subscribe, and hence not to publish:</p>
  
  <pre><code>sub.on("error", function (err) {
      console.log("Error " + err);
  });
  
  pub.on("error", function (err) {
      console.log("Error " + err);
  });
  
  Error AbortError: SUBSCRIBE can't be processed. The connection is already closed.
  </code></pre>