170

What's the difference between these two?

I noticed that if I changed from socket.emit to socket.send in a working program, the server failed to receive the message, although I don't understand why.

I also noticed that in my program if I changed from socket.emit to socket.send, the server receives a message, but it seems to receive it multiple times. When I use console.log() to see what the server received, it shows something different from when I use socket.emit.

Why this behavior? How do you know when to use socket.emit or socket.send?

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
node ninja
  • 31,796
  • 59
  • 166
  • 254

7 Answers7

147

With socket.emit you can register custom event like that:

server:

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

client:

var socket = io.connect('http://localhost');
socket.on('news', function (data) {
  console.log(data);
  socket.emit('my other event', { my: 'data' });
});

Socket.send does the same, but you don't register to 'news' but to message:

server:

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

io.sockets.on('connection', function (socket) {
  socket.send('hi');
});

client:

var socket = io.connect('http://localhost');
socket.on('message', function (message) {
  console.log(message);
});
Charles
  • 11,367
  • 10
  • 77
  • 114
  • 3
    It doesn't seem like a very big difference. It sees like socket.emit() can do everything that socket.on() can. Why did they have to make that function? – node ninja Jul 19 '12 at 23:20
  • 2
    I don't know, I checked the source code and they make almost the same (https://github.com/LearnBoost/socket.io/blob/master/lib/socket.js#L318). Maybe it's historical and just for backwards compatibility. – Charles Jul 20 '12 at 06:53
  • 1
    socket.io's making emit() do something other than what EventEmitter's emit() does is baffling, isn't it? It's a protocol on top of the raw pseudosockets that come out as named events on the far side, rather than raw messages. It lets you use a single socket for multiple streams, in a sense, multiplexing them. – aredridel Nov 25 '12 at 16:38
  • 44
    Also be aware, future readers of this post, that this is about socket.io, not node.js TCP, UDP or Unix sockets. – aredridel Nov 25 '12 at 16:42
  • 4
    Is it possible to emit with vanilla javascript Websockets? – Alex Buznik Aug 01 '14 at 13:47
  • 14
    Looks like `socket.send` is simply just an alias for `socket.emit('message', message);` – Mohit Gangrade Aug 17 '16 at 01:45
124

Simple and precise (Source: Socket.IO google group):

socket.emit allows you to emit custom events on the server and client

socket.send sends messages which are received with the 'message' event

Ricardo
  • 3,696
  • 5
  • 36
  • 50
M.D.
  • 1,886
  • 1
  • 13
  • 14
50

TL;DR:

socket.send(data, callback) is essentially equivalent to calling socket.emit('message', JSON.stringify(data), callback)

Without looking at the source code, I would assume that the send function is more efficient edit: for sending string messages, at least?

So yeah basically emit allows you to send objects, which is very handy.

Take this example with socket.emit:

sendMessage: function(type, message) {
    socket.emit('message', {
        type: type,
        message: message
    });
}

and for those keeping score at home, here is what it looks like using socket.send:

sendMessage: function(type, message) {
    socket.send(JSON.stringify({
        type: type,
        message: message
    }));
}
Specur
  • 3,240
  • 4
  • 23
  • 25
Kyle Shay
  • 688
  • 6
  • 9
28

socket.send is implemented for compatibility with vanilla WebSocket interface. socket.emit is feature of Socket.IO only. They both do the same, but socket.emit is a bit more convenient in handling messages.

artch
  • 4,487
  • 2
  • 28
  • 35
0

In basic two way communication systems, socket.emit has proved to be more convincing and easy to use (personal experience) and is a part of Socket.IO which is primarily built for such purposes

Krishna Ganeriwal
  • 1,903
  • 19
  • 17
0

https://socket.io/docs/client-api/#socket-send-args-ack

socket.send // Sends a message event

socket.emit(eventName[, ...args][, ack]) // you can custom eventName

Amit Gurbani
  • 577
  • 1
  • 4
  • 21
zhuxy
  • 140
  • 1
  • 5
0

update v4.x

According to the official docs:

socket.send - sends a message event. This can used like this:

// Client side
socket.send('hello')

// Server side
socket.on('message', (data) => {
   console.log(data) // hello
})

socket.emit - send a custom event. This is used like this:

// Client side
socket.emit('hello', 'rick')

// Server side
socket.on('hello', (name) => {
   console.log(name) // rick
})

I would personally recommend using socket.emit, as it's more simpler to understand, and keeps things consistent

Coder Gautam YT
  • 1,044
  • 7
  • 20