Multi threading in Node.js? Multi threading in Node.js? azure azure

Multi threading in Node.js?


As others have suggested, you can achieve this by using the setInterval or setTimeout functions to periodically emit data to connected clients. Although everything runs within the same controlling thread, all of the I/O is done asynchronously so your application will still be responsive. IE, any data received while the timer is running will be queued up and processed after the timer function returns.

See Timers in the node.js manual for more information.


On a side note, you will want to have your node.js application focus on processing asynchronous I/O in near real-time as that is the strength of node.js. If you need to do any heavy computations then you will want to offload that to another thread/process somehow, by simply making an async request for the result.


You are right, node is single threaded. But it makes heavy use of events.

The strategy you should go for is to listen for events on the connections, to retrieve data, and when you wish to send data back you do it because another event has been emitted.

If you take a look at the examples on the socket.io frontpage, you will see how they use events to start processing the streams. The on(eventName, function) method is how you listen for an event in NodeJS.

If you wish to do something based on timing (generally a bad idea), take a look at the setInterval method.

Server

    var io = require('socket.io').listen(80);    io.sockets.on('connection', function (socket) {      socket.emit('news', { hello: 'world' });      socket.on('my other event', function (data) {        console.log(data);      });    });

Client

    <script src="/socket.io/socket.io.js"></script>    <script>      var socket = io.connect('http://localhost');      socket.on('news', function (data) {        console.log(data);        socket.emit('my other event', { my: 'data' });      });    </script>


There is also a Node.js library for doing threads: node-webworker-threads

https://github.com/audreyt/node-webworker-threads

This basically implements the Web Worker browser API for node.js.

Note that this does not align with node.js's philosophy of single threadedness, but it's there if you need it.