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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
To do this you would want to listen to the
connectionevent on your server (as well asdisconnect) 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: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
disconnectevent 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.