0

I am learning about nodejs + express + socket.io. I have a list of user list, list get append automatically when clients are connected. For making trial I have checked with click event with div element.

client side

<div class="userlist">
  <ul>
    ............... ...............
  </ul>
</div>

<script>
  $('.userlist').click(function() {
    socket.emit('private', 'test');
  });

  socket.on('private', function(mesg) {
    $('body').append($('<li class="listm">').text(mesg));
  });
</script>

server side

io.on('connection', function(socket){
    socket.on('private', function(mesg)
    {  
       console.log('private test message');
       io.socket.emit('private',mesg);
    }); 
});

But it does not work. when I click div it is not print the message in console also it will not append any text.

what did I wrong?

1

1 Answer 1

2

I can't see anything wrong with the code you've shown but here's a few things I can think of:

1. Did you remember to link the http Server instance with socket.io?

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

And then using the linked server?

server.listen(port, () => {

});

2. Did you include socket.io on the client and connected to the server after the page loaded?

<script>
// < -- is there code here that does var socket = io('{server url}';
$('.userlist').click(function(){        
  socket.emit('private','test');
});

3. If yes, did you check if the connection succeeded?

// Put this on the client and look at the console
socket.on('connect', () => {
  console.debug('Connection to server established');
});

Another thing, even though it might just be a typo in the question, on the server you need to use the socket returned from the connection callback, not io.socket:

io.on('connection', function(socket){
    socket.on('private', function(mesg)
    {  
       console.log('private test message');
       socket.emit('private',mesg); // <----- Use this (without io.)
       // Not this: io.socket.emit('private',mesg); 
    }); 
});
Sign up to request clarification or add additional context in comments.

4 Comments

thanks dude io.socket.emit only the problem :-) it was my silly mistake. thanks to point out +1 from me :-)
How to debug error in nodejs? In above issue it does not throw any error in console. if debug option is there I figured out easily what was the error.
glad to help amigo, nothing like a fresh pair of eyes to show us our silly mistakes :) As far as no error being thrown, that's because there's already an object at io.socket that has an emit method, so technically you were just calling a wrong function which a debugger can't really help us much with
superb explanation I understood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.