Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using node.js to display number of current users

Using node.js to display number of current users


I've somehow managed to scrape together a local node server. All I'm trying to do is when a user connects to the server, to update an integer. I just can't find what keeps track of Current Users within the node.js code.

    currentUsers += 1;
else if(userDisconnects){
    currentUsers -= 1;

I'm very very new to server side programming (I've done a little php, but nothing that interacts directly with sever requests).

Problem courtesy of: RustyEight


You probably want to look at using It provides hooks into things that can easily Count and update the code.

I built an app that does this:

The source code is here:

See what I do with the count variable.

var count = 0
socket.on('connection', function(client) {
    client.on('disconnect', function(){

Hope that helps!

The client side code is here:

FYI, my app was built with an earlier version of so the syntax has changed slightly!

Solution courtesy of: Jamund Ferguson


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using node.js to display number of current users


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription