I need to implement long polling
for a chat application. I've searched around, but I only find how to implement it in JavaScript
using JQuery
. How can I implement it using only native JavaScript
and node.js
? Can you guide me to some relevant articles or materials?

- 2,443
- 4
- 33
- 33

- 929
- 3
- 12
- 24
-
I would suggest you take a look about sockets instead of long polling, its more efficient than long polling https://stackoverflow.com/questions/10028770/in-what-situations-would-ajax-long-short-polling-be-preferred-over-html5-websock (ps: I just recently knowed what is long polling so I cant help you with your question) – Frankusky Aug 24 '17 at 05:20
-
1hi , I am not allowed using sockets , it's a requirement in my project,although that sockets is more professional ,and I have used it in my previous project – user8244016 Aug 24 '17 at 05:22
1 Answers
Q: How to do long polling in native Javascript
in nodeJS
?
A: I guess first of all you need to understand how the long polling model works. If you haven't had any clue then the RFC-6202 specification is a good starting point.
It is about the client sending a request
to the server
and waits until a response is returned.
From the specification we know that first the client will have to issue a http
request which has an infinite or at least a high timeout value. Then the server, which is your nodeJs
application is expected to stash all incoming requests into a data structure, basically a holding area. Your application will essentially hold on all the response
object until an event gets triggered, then you reply to the responses appropriately.
Consider this Pseudo code:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var requestCounter = 0;
var responses = {
/* Keyed by room Id =*/
"room_abc" : [ /* array of responses */]
};
app.get('/', function (req, res) {
requestCounter += 1;
var room = /* assuming request is for room_abc */ "room_abc";
// Stash the response and reply later when an event comes through
responses[room].push(res);
// Every 3rd request, assume there is an event for the chat room, room_abc.
// Reply to all of the response object for room abc.
if (requestCounter % 3 === 0) {
responses["room_abc"].forEach((res) => {
res.send("room member 123 says: hi there!");
res.end();
});
}
});
app.use(bodyParser.text({ type: 'text/*' }));
app.use(bodyParser.json());
app.listen(9999, function () {
console.log('Example app listening on port 9999!')
})
It is relatively time consuming to write a working example here but the code above is a good example of how you can implement long polling in NodeJS
.
If you have postman
installed or curl
you can do HTTP
calls to http://localhost:9999/
using method GET
. You should noticed that on the first two calls you won't get a response and it is when you fired the 3rd one then you'll receive a response for all previous and current calls.
The idea here is you stash the request's response
object first and when an event comes through, assuming on every 3rd HTTP call, you then loop through all of the responses and reply to them. For your chat application's case, the event that triggers a response would probably be when someone fires off a message to a chat room.

- 1
- 1

- 18,006
- 3
- 24
- 39
-
hi and thank you for answering , but I have a question please, when some user send message to the room , can I replace the last condition with one that checks if there are new messages by counter value , that is if the counter value is bigger than what I already have then there is new message that should be shown on the room chat ,is that right? another thing please , how can I "call" the server in the client side using native javascript? – user8244016 Aug 24 '17 at 06:51
-
I guess you'll need to have two endpoints. One for the client to park and hold for responses. This is demonstrated above. Then another one for the client to post a chat message. When a message is written then you'll want to go through all of the `response` object for that room and reply them appropriately. – Samuel Toh Aug 24 '17 at 07:03
-
Regards to your other question on how a client can send make a `HTTP` rest call. Refer to https://developer.mozilla.org/en-US/docs/Web/API/Request for example – Samuel Toh Aug 24 '17 at 07:05
-
15Your code example doesn't show long polling, it actually shows something that is neither long polling or short polling. For it to be a long polling, if the data wouldn't be available while the request was made, the connection would be held and, when data becomes available, the response would be sent. If data doesn't become available before timeout, the connection should be aborted with a timeout. – Rafael Eyng Apr 30 '19 at 03:23