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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:39:39+00:00 2026-06-18T14:39:39+00:00

My node.js socket.io chat app is not emitting the chat message to other users

  • 0

My node.js socket.io chat app is not emitting the chat message to other users (when i open another browser). The message from browser A would only show up in browser A.

server.js

var app = require('http').createServer(handler),
     io = require('socket.io').listen(app),
     fs = require('fs');

app.listen(4000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.on('sendChat', function (data) {
    // console.log('sendChat', data);
    socket.emit('displayChat', data);
  });
});

index.html

<html>
<head>
  <title>Chat</title>

  <style type="text/css">
    #chats {
      overflow: auto;
    }

    #sender {
      position: absolute;
      bottom: 0;
    }
  </style>

  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="/socket.io/socket.io.js"></script>

  <script>
    $(document).ready(function(){
      var socket = io.connect('http://localhost');
      var chats = $("#chats ul");

      socket.on('displayChat', function (data) {
        // console.log("displayChat", data);
        chats.append($("<li></li>").text(data));
      });

      var author = $("#author");
      var message = $("#message");

      message.keypress(function(event) {
        if ((event.keyCode || event.which) == 13 && author.val() != '') {
          // console.log("sendchat", author.val() + ": " + message.val());
          socket.emit('sendChat', author.val() + ": " + message.val());
          message.val('');
        }
      });
    });
  </script>
</head>
<body>

  <div id="chats">
    <ul></ul>
  </div>

  <div id="sender">
    <input id="author" type="text"><input id="message" type="text">
  </div>

</body>
</html>

here is a log

 info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized T0NOrPaqIkQMXOJjgz9E
   debug - setting request GET /socket.io/1/websocket/T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - client authorized for 
   debug - websocket writing 1::
sendchat a: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["a: hi"]}
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized PL2VRwE2V0UvBc6dgz9F
   debug - setting request GET /socket.io/1/websocket/PL2VRwE2V0UvBc6dgz9F
   debug - set heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
   debug - client authorized for 
   debug - websocket writing 1::
sendchat b: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
   info  - transport end (socket end)
   debug - set close timeout for client PL2VRwE2V0UvBc6dgz9F
   debug - cleared close timeout for client PL2VRwE2V0UvBc6dgz9F
   debug - cleared heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
   debug - discarding transport
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized I3S_VVYyencv9VQOgz9G
   debug - setting request GET /socket.io/1/websocket/I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
   debug - client authorized for 
   debug - websocket writing 1::
sendchat b: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat a: digg
   debug - websocket writing 5:::{"name":"displayChat","args":["a: digg"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat b: dd
   debug - websocket writing 5:::{"name":"displayChat","args":["b: dd"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
  • 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-18T14:39:41+00:00Added an answer on June 18, 2026 at 2:39 pm

    With socket you are emiting only to the current connection. You need to emit using io.sockets and it will be emitted to any open socket.

    Change this:

    io.sockets.on('connection', function (socket) {
      socket.on('sendChat', function (data) {
        // console.log('sendChat', data);
        socket.emit('displayChat', data);
      });
    });
    

    To this:

    io.sockets.on('connection', function (socket) {
      socket.on('sendChat', function (data) {
        // console.log('sendChat', data);
        io.sockets.emit('displayChat', data);
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a node.js chat app that uses socket.io and other dependencies but I
I'm writing a multi-room chat app with Node.js and Socket.IO and I'm wondering how
Lets say that i make a websocket server chat (node.js + socket.io). How would
I'm working on a chat application with Node.js and Socket.io, and on disconnect, the
I am building a small app that uses backbone.js on the client side, node.js/socket.io
I'm implementing a node.js server that manages bi-connections between users with socket.io (version:0.8.7). I
I have hosted an app on appfog.com. Its a chat program(I am a node
I've got a chat program which pushes JSON data from Apache/PHP to Node.js, via
I'm creating an app that uses Rails to serve webpages and node.js/socket.io to facilitate
I'm making a little Node app using socket.io 0.9.13 using Express 3.0.6. I can't

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.