It is difficult to know exactly what the situation is without seeing the routes / actions in your main application ...
However, I believe that the problem you are working with is that you are trying to send two sets of headers to the client (browser), which is prohibited. The reason this is unacceptable is because the browser does not allow you to change the type of response content after sending the initial response ... because it uses this as an indicator of how to handle the response you send it to. You cannot change any of these (or any other headers) after you send them to the client once (one request → one response → one set of headers back to the client). This prevents your schizophrenic from appearing (switching from the “200 Ok” to “400 Bad Request” response), for example.
In this case, at the first request, you tell the client "Hey, that was a valid request, and here is my answer (via status 200, which is either set elsewhere or ExpressJS is assumed), and please keep the communication channel open so that I could send you updates (by setting your content type to text/event-stream ). "
There are many options for how to “fix” this. When I did this, I used the pub / sub redis function to act as a “pipe” that connects everything. So the stream was like this:
Some clients send a request to /your-event-stream-url
In this request, you configured your Redis subscriber. Everything included in this subscription can be processed as you wish. In your case, you want to "send some data through the pipe to the client in a JSON object with the data attribute". After you have configured this client, you simply return the response "200 Ok" and set the content type to "text / event-stream". Redis takes care of the rest.
Then another request is made to another URL endpoint, which performs the “host JSON object” task by pressing /your-endpoint-that-processes-json. (Note: obviously, this request can be made using the same user / browser ... but the application does not know / cares about it)
In this action, you process your JSON data, increment your counters or do whatever ... and return a 200 response. However, one of the things you do in this action is to “post” a message on the Redis channel that your Subscribers from the first step listen so that customers receive updates. Technically, this action does not need to return anything to the client, assuming that the user will have some kind of feedback based on the status code 200 or on the event sent by the server, which is sent over the channel ...
The tangible example that I can give you is the meaning , which is part of this article . Please note that this article is a couple of years old, so some of the code may need to be slightly corrected. Also note that this is not guaranteed to be anything more than an example (i.e., it was not “load tested” or something like that). However, this may help you get started.
source share