Skip to content

Instantly share code, notes, and snippets.

@developeryashraj
Last active December 13, 2018 12:26
Show Gist options
  • Save developeryashraj/47831ffbc4eb4515d99fb6dda22b1e21 to your computer and use it in GitHub Desktop.
Save developeryashraj/47831ffbc4eb4515d99fb6dda22b1e21 to your computer and use it in GitHub Desktop.
Socket.IO with MQTT on Node. Single Threaded Vs Multi Threaded
let SocketPublishIO_Single = function (topic_or_room, data) {
if (io.sockets.adapter.rooms[topic_or_room]) { // if room is available then emit in the room rather doing broadcast
// console.log("Emitting to socket room ",topic_or_room);
io.to(topic_or_room).emit(topic_or_room, data);
} else {
// No room. Do broadcast.
// console.log("Emitting without room ",topic_or_room);
io.emit(topic_or_room, data);
}
}
let SocketPublishIO_Multi = function (topic_or_room, data) {
// helpful url for redis adapter https://github.com/socketio/socket.io-redis
io.in(topic_or_room).clients((err, clients) => {
if (err) {
//console.log("Error");
} else if (clients.length > 0) { // If any clients found for that room than consider it as room emit else broadcast
// console.log("Emitting to socket room " + topic_or_room, "Worker id is :- " + cluster.worker.id);
io.to(topic_or_room).emit(topic_or_room, data);
} else {
// No room. Do broadcast.
// console.log("Emitting without room " + topic_or_room, "Worker id is :- " + cluster.worker.id);
io.emit(topic_or_room, data);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment