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 7823169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:09:10+00:00 2026-06-02T08:09:10+00:00

In my normal setup, the client will emit data to my server regardless of

  • 0

In my normal setup, the client will emit data to my server regardless of whether or not there is another client to receive it. How can I make it so that it only sends packets when the user-count is > 1? I’m using node with socket.io.

  • 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-06-02T08:09:12+00:00Added an answer on June 2, 2026 at 8:09 am

    To do this you would want to listen to the connection event on your server (as well as disconnect) and maintain a list of clients which are connected in a ‘global’ variable. When more than 1 client is connected send out a message to all connected clients to know they can start sending messages, like so:

    var app = require('express').createServer(),
        io = require('socket.io').listen(app);
    
    app.listen(80);
    
    //setup express
    
    var clients = [];
    
    io.sockets.on('connection', function (socket) {
        clients.push(socket);
    
        if (clients.length > 1) {
            io.socket.emit('start talking');
        }
    
        socket.on('disconnect', function () {
            var index = clients.indexOf(socket);
    
            clients = clients.slice(0, index).concat(clients.slice(index + 1));
    
            if (clients.length <= 1) {
                io.sockets.emit('quiet time');
            };
        });
    });
    

    Note: I’m making an assumption here that the socket is passed to the disconnect event, I’m pretty sure it is but haven’t had a chance to test.

    The disconnect event wont receive the socket passed into it but because the event handler is registered within the closure scope of the initial connection you will have access to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've wrote a simple client and a server. They both can configured to use
In normal mode I can hit Ctrl + E which deletes the rest of
Generating normal columnar data in excel file is quite easy but does any one
If you make a normal table with 100% width and give the cells some
I am trying to make use of the PHP driver for MS SQL Server
I was wondering if it was possible to setup the HttpListener to process normal
This is a very simple setup, I can't believe but I didn't find anybody
Context I have a very normal setup in my app: an NSOutlineView (backed by
So, the setup is this, 2 separate servers... Web server, has IIS7, MS SMTP
With a single-server setup for simplicity, which steps could you do to minimize the

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.