Socket.io broadcasting not working
<p>I'm really new to node.js and I'm starting to learn it. Actually I'm trying to develop a simple chat application just for learning purpose.
I'm also using socket.io package.
That's what I've done:
1) index.js</p>

<pre><code>var express = require('express');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var users = {};
var socket = io.listen(3001);
var dateFormat = require('dateformat');

app.use('/public',express.static(path.join(__dirname, '/public')));

app.get('/',function(req,res){
    res.sendFile(__dirname+'/index.html')
});

//CHAT
socket.on('connection',function(client){        
    client.on('join',function(name){   
        var date = dateFormat(new Date(),"HH:MM:ss dd-mm-yyyy");
        var hour = dateFormat(new Date(),"HH:MM:ss");
        users[client.id] = {"name":name,"id":client.id,"status":1,"joinDate":date};       
        client.emit("update", "You have connected to the server.");
        var msg ={'msg':"Client: "+name+" joined the channel", 'user':name,'hour':hour,'fromServer':1};   
        io.sockets.emit('update', msg);          
    });
    client.on('request-list',function(){       
        client.emit('user-list',users);
    });
    client.on('chat',function(msg){
            //socket.broadcast.emit('update',msg);
            //THIS DOESN'T WORK
            socket.emit('update', msg);       
    });

    client.on('disconnect',function(){       
        delete users[client.id]; 
        if(Object.keys(users).length&gt;0){
            console.log(Object.keys(users).length);
        } else{
            console.log("NESSUNO");
        }     
    });  
});
//END CHAT

http.listen(3000,function(){
    console.log('listening on 3000');
});
</code></pre>

<p>2) main.js</p>

<pre><code>$(document).ready(function(){
    var socket = io.connect('127.0.0.1:3001');    
    var chatStarted = false;    
    $('[data-type="loginMask"] [data-component="login-button"]').off().on('click',function(){
        socket.emit('join',$('[data-component="login-nickname"]').val());
        localStorage.setItem('user',$('[data-component="login-nickname"]').val());                
    });
    socket.on('update',function(msg){
        if(!chatStarted){
            $('[data-type="loginMask"]').hide();
            $('[data-type="chatMask"]').show();
            socket.emit('request-list'); 
            chatStarted=true;
        }else{                 
            $('[data-component="chat"] [data-component="chat-messages"]').append(formatMessage(msg));            
        }       
    });
    socket.on('user-list',function(list){       
        var html ='';
        $.each(list,function(k,item){
            html += '&lt;div data-component="userlist" data-id="'+item.id+'"&gt;';
            html += '&lt;span data-component="userlist-status"&gt;'+getStatus(item.status)+'&lt;/span&gt;';
            html += '&lt;span data-component="userlist-user"&gt;'+item.name+'&lt;/span&gt;';
            html += '&lt;span data-component="userlist-caret"&gt;&lt;i class="fa fa-caret-right" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;';
            html += '&lt;/div&gt;';
        });
        $('[data-type="userlist"]').append(html).promise().done(function(){

        });
    });
    $('[data-component="chat-text"]').off().on('keydown',function(e){
        if(e.which==13){
            $('[data-component="chat-send"]').trigger('click');
        }
    });
    $('[data-component="chat-send"]').off().on('click',function(){       
        var msgToSend = {'msg':$('[data-component="chat-text"]').val(),'user':localStorage.getItem('user'),'fromServer':0}; 
        $('[data-component="chat"] [data-component="chat-messages"]').append(formatMessage(msgToSend)); 
             //socket.broadcast.emit('chat',msg);
            //THIS DOESN'T WORK       
        socket.emit('chat',msgToSend);
        $('[data-component="chat-text"]').empty();
    });
});
</code></pre>

<p>I tried to use the broadcast function in two parts of the code (where there's the comment THIS DOESN'T WORK :), but  I always receive undefined in both parts. If I leave the code as it's now the message will be sent to all clients included the sender, I want to send the message to everyone except the client.
As I said I'm new to node and socket.io as well, so I can't really understad what is the problem.
In the socket.io chat example <a href="http://socket.io/get-started/chat/" rel="nofollow">here</a> they made use of broadcast, so I guess it's a method of the socket.</p>

<p>Thank you in advance for your help</p>