I am writing the server-side portion of an application in NodeJS with Express, which will use Server-Sent-Events (SSE). I have a solution that seems to work, but need to understand whether there is a better way of doing it in Node.
The relevant portion of code currently looks like this:
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
};
res.writeHead(200, headers);
// Do more stuff [...]
where res
is the response object.
Instead of calling res.writeHead()
, I have seen other examples use res.flushHeaders()
:
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.flushHeaders();
Is there a difference between the two and is one preferable to the other, with respect to SSE and keeping a connection open to send messages?
I am writing the server-side portion of an application in NodeJS with Express, which will use Server-Sent-Events (SSE). I have a solution that seems to work, but need to understand whether there is a better way of doing it in Node.
The relevant portion of code currently looks like this:
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
};
res.writeHead(200, headers);
// Do more stuff [...]
where res
is the response object.
Instead of calling res.writeHead()
, I have seen other examples use res.flushHeaders()
:
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.flushHeaders();
Is there a difference between the two and is one preferable to the other, with respect to SSE and keeping a connection open to send messages?
Share Improve this question edited May 14, 2020 at 14:26 Widor asked May 14, 2020 at 14:21 WidorWidor 13.3k7 gold badges47 silver badges67 bronze badges 1- Do you get chunks of data at client side when you do res.writeHead ? Or do you get chunks of data at client side with res.flushHeaders ? – Shiv Commented Dec 29, 2020 at 9:54
2 Answers
Reset to default 2I had the same question. In addition to Walters response I found the following:
writeHead
and flushHeaders
are both methods expressjs
uses from nodejs
and you don't need to use one of those two to properly implement SSE
. For passing headers you'll need writeHead
or setHeader
.
response.flushHeaders()
is used when you need to bypass buffering the request headers. From testing, I can confirm it's not needed for SSE
to work properly. So using it will be based on your specific application munication needs, not the needs of SSE
.
Using response.writeHead()
is a preferred alternative to using setHeader
. If progressive population of headers is desired with potential future retrieval and modification, use response.setHeader()
instead.
Neither writeHead
or flushHeaders
are used to keep the connection open. The connection will stay open by default until the client disconnects or you close it on the server with response.end()
or response.send()
which will call end
. The exception to that is the HTTP default timeout.
Explicitly specifying a statusCode
code is based on your usage but not needed (unless you use response.writeHead()
). For example you can use a response code to tell a client to stop reconnecting by using a HTTP 204 No Content response code.
Note: For expressjs
users using pression
, make sure you call the res.flush()
method (provided by the pression
middleware) after your res.write()
per the docs.
Here is a repo with plete example. For the server sse
portion see this file.
They are practically equivalent because res.write()
flushes the headers along with its first chunk. For SSE you're very likely needing an immediate response, at the very least to indicate successful connection. res.setHeader()
can be useful if you later need to access or modify header state, but that doesn't seem relevant to SSE.
See nodejs documentation for response.write() and response.setHeader().
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745490065a4629949.html
评论列表(0条)