For some reason, every time a new socket connects to the server, my emitDraw function stalls midway through a draw(the process.nextTick() call cycle gets broken unexpectedly). Is there anyway to keep my emitDraw function running while also accepting new connections?
io.sockets.on('connection', function(socket) {
socket.on('drawing', function() {
//some code
emitDraw(socket);
});
});
function emitDraw(socket) {
//some code
process.nextTick(function(){emitDraw(socket)});
};
Thanks
process.nextTick() merely helps pushing code off the current executing stack to the top of the next one. Callbacks for events fired between these two stack executions are therefore allowed execution in the next stack.
In the current case, a normal executing stack only has emitDraw() executing and the call to process.nextTick() allows for events (notably the socket connection) to fire and execute in the next stack. Once the socket connection is fired the stack has two sets of code to execute, the callback associated with socket.on(connection) and emitDraw().
If emitDraw() turns unresponsive, it only means that it cannot co-execute on the current stack without performance degradation.
Since the main Node.js process runs on a single thread you cannot expect any better performance by keeping it in the same thread, except by increasing processing power. Alternatively you could fork emitDraw() to a child process so that it can execute independent of the main process.