Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a socket that simply emits and one that volatile emits?

This is a follow-up question to the question below;

Why does this updateSockets() function accept a parameter that look like this?

In the code below, the socket uses volatile to emit.

var updateSockets = function(data) {
  // adding the time of the last update
  data.time = new Date();
  console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  // sending new data to all the sockets connected
  connectionsArray.forEach(function(tmpSocket) {
    tmpSocket.volatile.emit('notification', data);
  });
};

What if the code is changed such that it becomes tmpSocket.emit('notification', data);? What is the difference between tmpSocket.volatile.emit('notification', data); and tmpSocket.emit('notification', data);?

like image 289
guagay_wk Avatar asked Jul 29 '15 10:07

guagay_wk


People also ask

What is difference between socket and IO?

Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.

What is socket broadcast?

Broadcasting means sending a message to all connected clients. Broadcasting can be done at multiple levels. We can send the message to all the connected clients, to clients on a namespace and clients in a particular room. To broadcast an event to all the clients, we can use the io.

How does Socket.IO work?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.

What is the use of socket join?

You can call the join method on the socket to subscribe the socket to a given channel/room. For example, let us create rooms called 'room-<room-number>' and join some clients. As soon as this room is full, create another room and join clients there. Note − We are currently doing this on the default namespace, i.e. '/'.


1 Answers

From the Socket.io docs:

Sending volatile messages

Sometimes certain messages can be dropped. Let’s say you have an app that shows realtime tweets for the keyword bieber.

If a certain client is not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and is in the middle of a request-response cycle), if they doesn’t receive ALL the tweets related to bieber your application won’t suffer.

In that case, you might want to send those messages as volatile messages.

Essentially, if you don't care if the client receives the data, then send it as volatile.

like image 192
SlashmanX Avatar answered Sep 28 '22 04:09

SlashmanX