Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:06:59+00:00 2026-05-30T19:06:59+00:00

I’m writing a multi-room chat app with Node.js and Socket.IO and I’m wondering how

  • 0

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.

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.

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.)

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.

Here is the relevant client code:

socket.on("connect", function(){
    socket.emit("join", { username: username, room: room });
});

And on the server:

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 && data.room) {
        socket.set("data", data);
        socket.join(data.room);
        if(!buffer[data.room]) buffer[data.room] = [];
        socket.broadcast.to(data.room).emit("message", "<em>" + data.username + " has joined room " + data.room + "</em>");
        socket.emit("message", "<em>You have joined room " + data.room + "</em>");
        socket.emit("message", buffer[data.room]);
    }
});

socket.on("message", function(message) {
    if(message) {
        socket.get("data", function(err, data){
            if(data) {
                var msg = "<span>" + data.username + ":</span> " + message;
                buffer[data.room].push(msg);
                if(buffer[data.room].length > 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", "<em>" + data.username + " has left room " + data.room + "</em>");
        }
    });
});

});

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:07:01+00:00Added an answer on May 30, 2026 at 7:07 pm

    Perhaps I’m missing something, but wouldn’t this work.

    Client:

    var firstJoin = true;
    socket.on("connect", function(){
        socket.emit("join", { username: username, room: room, firstJoin: firstJoin });
        firstJoin = false;
    });
    

    Server:

    io.sockets.on("connection", function(socket) {
        socket.on("join", function(data){
            if(data.username && data.room) {
            socket.set("data", data);
            socket.join(data.room);
            if(!buffer[data.room]) buffer[data.room] = [];
    
            if (data.firstJoin) {
                socket.broadcast.to(data.room).emit("message", "<em>" + data.username + " has joined room " + data.room + "</em>");
                socket.emit("message", "<em>You have joined room " + data.room + "</em>");
                socket.emit("message", buffer[data.room]);
            }
        }
    });
    

    As for the fact that the connection event occurs on every new poll, that’s an unavoidable consequence of XHR polling. Since each poll is a new HTTP request, there is no state from the previous request, so you need to set it up again.

    If you want to reduce how frequently this happens, you could try increasing the polling duration from its default of 20. For example:

    io.configure(function() {
        io.set('polling duration', 60);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.