title: How do I store persistent data with xhr-polling in Socket.IO?
body: <p>I'm writing a multi-room chat app with Node.js and Socket.IO and I'm wondering how to handle data that needs to persist for the duration of a session, when the transport is xhr-polling.</p>
  
  <p>Currently, on the client I'm sending a "join" event on connection, which passes a long a few variables (such as username), which I store server-side using Socket.IO's get and set methods. They are then available until the client disconnects.</p>
  
  <p>This works fine if the transport is Websockets, but if it falls back to xhr-polling, the join event is emitted on each connect, which happens every 5-10 seconds. (Similarly, "such and such has left/joined the room" is sent on each xhr-poll request, which is also undesirable.)</p>
  
  <p>I'm not sure what the best way forward is. I can't disable xhr-polling as it's a needed fallback for flashless IE for one.</p>
  
  <p>Here is the relevant client code:</p>
  
  <pre><code>socket.on("connect", function(){
      socket.emit("join", { username: username, room: room });
  });
  </code></pre>
  
  <p>And on the server:</p>
  
  <pre><code>var io = require("socket.io").listen(8124)
  , buffer = {}
  , max_room_buffer = 15;
  
  io.sockets.on("connection", function(socket) {
      socket.on("join", function(data){
          if(data.username &amp;&amp; data.room) {
          socket.set("data", data);
          socket.join(data.room);
          if(!buffer[data.room]) buffer[data.room] = [];
          socket.broadcast.to(data.room).emit("message", "&lt;em&gt;" + data.username + " has joined room " + data.room + "&lt;/em&gt;");
          socket.emit("message", "&lt;em&gt;You have joined room " + data.room + "&lt;/em&gt;");
          socket.emit("message", buffer[data.room]);
      }
  });
  
  socket.on("message", function(message) {
      if(message) {
          socket.get("data", function(err, data){
              if(data) {
                  var msg = "&lt;span&gt;" + data.username + ":&lt;/span&gt; " + message;
                  buffer[data.room].push(msg);
                  if(buffer[data.room].length &gt; max_room_buffer) buffer[data.room].shift();
                  io.sockets.in(data.room).emit("message", msg);
              }
          });
      }
  });
  
  socket.on("disconnect", function () {
      socket.get("data", function(err, data){
          if(data) {
              socket.leave(data.room);
              socket.broadcast.to(data.room).emit("message", "&lt;em&gt;" + data.username + " has left room " + data.room + "&lt;/em&gt;");
          }
      });
  });
  
  });
  </code></pre>
  
  <p>Thanks in advance.</p>