I am trying to implement a simple long-term survey service for use in my own projects and may release it as SAAS if I succeed. These are the two approaches I've tried so far using Node.js (PostgreSQL poll in the opposite direction).
1. Periodically check all clients for one interval
Each new connection is placed in the connection queue through which the interval passes.
var queue = []; function acceptConnection(req, res) { res.setTimeout(5000); queue.push({ req: req, res: res }); } function checkAll() { queue.forEach(function(client) {
2. Check each client for a separate interval
Each client receives their own ticker , which checks for new data.
function acceptConnection(req, res) { // something which periodically checks data for the client // and responds if there is anything new new Ticker(req, res); }
While this keeps the minimum latency for each client lower, it also introduces overhead by setting a lot of timeouts.
Conclusion
Both of these approaches solve the problem quite easily, but I donβt feel that it scales easily to 10 million open connections, especially since I check the database for each check for each client.
I thought about it without a database and immediately broadcast new messages for all open connections, but this will not work if the client connection dies in a few seconds during the broadcast, because it is not permanent. This means that I basically should be able to search for messages in the story when the client first polls.
I think one step here was to have a data source in which I can subscribe to new data entering the system (CouchDB change notifications?), But maybe I missed something in the big picture here?
What is the usual approach for a large-scale long survey? I am not attached to Node.js, I would prefer any other sentence with an argument why.