-1
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
  console.log('listening on *:3000');
});
app.use('/js',express.static(__dirname+'/js'));
app.get('/', function(req, res){
    res.sendfile('codeschool_chattr.html');
});
io.on('connection', function(client){
  console.log('a user connected');
  var nick="";
  client.on('join',function(name){
    client.nickname=name;
  });
  client.on('disconnect', function(){
    console.log('user disconnected');
  });
  client.on('chat message', function(msg){
    client.broadcast.emit('chat message', msg);
  });
});

I have a socket io application code, here the chat message is being sent to the all the clients connected except the one who sent it, what should i do or what did i miss to make that happen. also here is the front end script of mine -

<script src="/socket.io/socket.io.js"></script>
    <script>
      var server = io.connect('http://localhost:3000');
      server.on('connect',function(data){
        var nick = prompt(" What do your friends call you? ");
        server.emit('join',nick);
      });
      $('form').submit(function(){
        server.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      server.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
Harshit Laddha
  • 2,044
  • 8
  • 34
  • 64
  • Try searching old questions before posting a new one : http://stackoverflow.com/questions/7352164/update-all-clients-using-socket-io/7352443#7352443 – user568109 Jun 09 '14 at 07:52

1 Answers1

1

try

client.on('chat message', function(msg){ io.sockets.emit('chat message', msg); });

This should send to all connected clients.

Holybreath
  • 403
  • 2
  • 8