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

The Archive Base Latest Questions

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

I’ve been staring at node.js and socket.io examples all afternoon and i’m trying to

  • 0

I’ve been staring at node.js and socket.io examples all afternoon and i’m trying to piece together a simple page that will tell me how many users I have connected to the server.

I have read the documentation at http://socket.io/ as well as a few tutorials / questions here that outline exactly what i’m trying to do. I have also found create simple node js server and client which does not help me.

Version Information:

node.js – 0.6.15
express – 3.0.0alpha1
socket.io – 0.9.5 (socket.io-client is the same version, however does not find the resource… see comments)
ejs – 0.7.1

Here is my server code:

var express = require('express'),
    config = {
        port: 4000,
        hostname: 'localhost'
    };

var server = module.exports = express.createServer();
    /* server configuration */
    server.use(express.cookieParser('keyboard unicorn'));
    server.use(express.bodyParser());
    server.use(express.methodOverride());
    server.use(express.session({ secret: 'keyboard unicorn' }));
    server.engine('.html', require('ejs').__express);
    server.set('views', __dirname + '/lib/views');
    server.set('view options', { layout: false });
    server.set('view engine', 'html');
    server.use(server.router);
    server.use('/', express.static(__dirname + '/lib/assets'));

var io = require('socket.io').listen(server);

var connections = { 'length': 0 };

io.sockets.on('connection', function(socket) {
    socket.__fd = socket.fd;
    connections[socket.__fd]=socket.remoteAddress;
    ++connections.length;
    console.log('user connected! There are a total of ' + connections.length + ' users online.');
    return socket.on('disconnect',function(){
        delete conns[socket.__fd];
        --connections.length;
        console.log('user disconnected! There are a total of ' + connections.length + ' users remaining online.');
    });
});

server.get('/', function( req, res ) {
    res.render('index', {
        'page_title': 'sample application',
        'number_of_connections': connections.length
    });
});

server.listen(config.port, config.hostname);

Here is my client code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><%= page_title %></title>
</head>
<body>
<div>There is a total of <%= number_of_connections %> user(s) connected.</div>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script type="text/javascript">
var socket = new io.Socket('localhost',{'port':4000});
socket.connect();
socket.on('connect', function() {
    console.log('connected');
});
</script>
</body>
</html>

Here is what is happening when I run the server, and connect with the client.

$ node server.js
   info  - socket.io started

then when I navigate to localhost:4000 on my web browser I get the page with ‘0’ (number_of_connections). Also, I see nothing in the terminal for the server (the on.(‘connection’ is never hit).

On the client, after a second or two, I start to get a massive number of errors (if I leave the console open for a few seconds it crashes the page) see image below:

enter image description here

Please, any help with where to start for debugging this would be appreciated! I just want to get this basic example up and running so I can start playing with / understanding nodejs and 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:26:09+00:00Added an answer on June 2, 2026 at 8:26 am

    The problem is because of Express now being a function.

    You need to do the following:

    var express = require('express');
    var app = express();
    var server = app.listen(3000);
    
    var io = require('socket.io').listen(server);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.