I am implementing an application that uses websockets and a console accessed via Telnet. There is a communication between the connection established via websockets and the console. I am experiencing a weird issue:
I share here the nodeJS code, take into account that this is now a test app so it's assumed only one socket and websockets connection:
// Sample based on: http://elegantcode.com/2011/05/04/taking-baby-steps-with-node-js-websockets/ // Changed for sockets.io 6.x => 7.x var events = require('events'); var eventEmitter = new events.EventEmitter(); var http = require('http'); var socketIO = require('socket.io'); var static = require('node-static'); var port = 2000; var clientFiles = new static.Server('./client'); var httpServer = http.createServer( function(request, response) { request.addListener('end', function() { clientFiles.serve(request, response); }); }) httpServer.listen(port); console.log("Server running at port: " + port); var io = require('socket.io').listen(httpServer); var webSocket = io.sockets; webSocket.on('connection', function(client) { client.send('Please enter a user name ...'); var userName; eventEmitter.on('someOccurence', function(message) { console.log("Received: " + message); client.send('Line Received'); var s = 'popo'; client.send(s); //client.send(message); }); client.on('message', function(message) { console.log(message); if(!userName) { userName = message; client.broadcast.send(message + ' has entered the zone.'); return; } var broadcastMessage = userName + ': ' + message; client.broadcast.send(broadcastMessage); client.send("Repeated here: " + message); }); client.on('disconnect', function() { var broadcastMessage = userName + ' has left the zone.'; client.broadcast.send(broadcastMessage); }); }); var lines = require('lines-adapter'); var net = require('net'); var server = net.createServer(function (socket) { socket.write("Welcome to the Server\r\n"); lines(socket, 'utf8') .on("data", function(line) { console.log("Line received: " + line); eventEmitter.emit('someOccurence', line); } ) .end("end", function() { console.log("End of Stream"); } ); }); server.listen(1337, "127.0.0.1");
UPDATE: I just tested with socket.io 0.8.6 and nodeJS 0.4.12 without this issue. I will keep this question here for future reference.
Socket.IO is way more than just a layer above WebSockets, it has different semantics (marks messages with name), and does failovers to different protocols, as well has heartbeating mechanism. More to that attaches ID's to clients on server side, and more. So it is not just a wrapper, it is full-featured library.
The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
All in all, the Socket.io code was incredibly simple.
Scalability problems with Socket.IOjs (and therefore Socket.IO) is single threaded, meaning that it cannot take advantage of multi-core processors. In order to do this, we need to run a separate Node. js instance for each core. However, these instances would be completely independent and not share any data.
Fixed with socket.io v0.8.6. This q should be answered/closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With