|
|
|
|
|
by greggman
4082 days ago
|
|
> If you send json over the wire, socket.io automatically converts it to a javascript object or array for you, ready to be used. That's literally 4 lines // sender
ws.send({event: JSON.stringify(someObject));
// receiver
ws.on('message', function(str) {
obj = JSON.parse(str);
});
> socket.emit("hello"); and the server can listen on this event with socket.on("hello").This is hardly any more lines of code // sender
function sendEvent(eventName, data) {
ws.send(JSON.stringify({event: eventName, data: data}));
}
// receiver
ws.on('message', function(str) {
var eventInfo = JSON.parse(str);
eventEmittier.emit(eventInfo.event, eventInfo.data);
});
Sure I'm glossing over a few things but EventEmitter is 10-20 lines depending on how crazy you want to get and wrapping both in a objects is probably another 10-20 linessocket.io on the other hand is a C node.js plugin and 61000 lines of JS code :( |
|
Sure, you can bolt all of that on in probably a few hundred lines, but socket.io also doesn't just do sockets and events :)
(And it also does it in less than 61000 lines. You still need the "ws" module to do WebSocket stuff in Node, even if you don't use socket.io)