We are a Java company. But we had a project where Java was not ideal: a real-time dashboard with thousands of simultaneous WebSocket connections. Node.js with its event loop model scales better than thread-per-connection.
Event-driven, non-blocking¶
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.on('subscribe', function(lineId) {
socket.join('line-' + lineId);
});
});
function broadcastSensorData(lineId, data) {
io.to('line-' + lineId).emit('sensor-update', data);
}
The NPM ecosystem and callback hell¶
Over 100,000 packages — both a strength and a weakness. Quality varies. Callback hell is solved by Promises; we are waiting for async/await. IDE support is weaker than for Java — WebStorm is the best choice.
Coexistence with Java¶
Java for business logic and the REST API, Node.js for the WebSocket gateway. Redis pub/sub connects the two runtimes. Each does what it does best.
Polyglot is the future¶
Node.js is not a Java killer. It is an excellent tool for I/O-intensive scenarios. The key is not to be afraid to use the right tool for the right problem.
Need help with implementation?
Our experts can help with design, implementation, and operations. From architecture to production.
Contact us