Closed AnhTuan-AiT closed 3 years ago
The connection is still open normally, the client still receives the heartbeat events in full and at the right time
Sounds, like this is a bug in the library, I would try to debug the js code, please put a break point on the place that throws the exception and looks what is going on.
Please give me your mail, I will give u my app address and account so u can debug directly
Please check your mail. I sent the infomation to u
@AnhTuan-AiT , nothing
@Yaffle I don't know the reason but could u send me an email to: anhtuan0126104@gmail.com and I will reply to it
@AnhTuan-AiT , it works for me there is an unclear delay before the open event when I am using Firefox
After trying to find the cause of the error, I obtained the following symptoms.
Before talking about the phenomenon, I will describe how my application works. When the user accesses the application, I will check the token in localStorage, if exists (regardless of whether the token is valid or not) then the application's state will be set as logged in and the component containing the Event Source object will be rendered and initiate a connection to the server with an expired token placed in the headers. And of course it can't connect, then the onerror callback function will be called and the reconnect function will be called. And here is the next phenomenon:
When I press the login button, the application goes to the login screen and of course the component containing the Event Source object is unmounted, but when I look at the console, what I see is the reconnect function keeps executing.
I have a theory and to test the theory, I added a flag value as follows:
headers: {
"X-Auth-Token": store.getState().auth.token,
Count: count++,
},
Here, count will be incremented by 1 every time the reconnect function is called. And after I have successfully logged in, got a valid new token, successfully connected to the server, usually every 3 heartbeat events, I get the error: No activity within N milliseconds. 96 characters received. Reconnecting. Thanks to the count variable, I know that the error was thrown from the Event Source object that failed in the previous connection, when I wasn't logged in.
When I remove the reconnect. At the time I access the application, of course the Event Source will not be able to connect and the onerror callback function is called, but this time there is no reconnect function so only an Event Source object is created. After logging in, I see the Event Source successfully connected to the server and received the heartbeat events fully and in the correct cycle, no more errors.
I don't know why the reconnect function can continue to run when the component is unmounted and why the previous Event Source objects continue to exist
@AnhTuan-AiT , it works for me there is an unclear delay before the open event when I am using Firefox
On chrome I also see this phenomenon, maybe this is a backend issue but I don't think it's a big deal. Maybe I'll try to determine the cause and fix this later
@AnhTuan-AiT looks like you need to disconnect using the "close" method when you are unmounting the component. I think, It will not be closed automatically because you have listeners attached which print to the console.
In useEffect, I return a clean up function:
return () => {
eventSource.close();
console.info("SSE closed");
};
Is it not working?
Or do you mean I have to collect all created EventSource objects and call close() method on all of them
no, only on the one you want to close, don't know if useEffect works
I use the reconnect func just because I want to control the speed of the connection again, avoiding the server being overloaded due to repeated reconnections with short cycles. When I don't use it, as commented above, everything works OK, no errors occur. Because I know there are some cases where the server-sent-event won't try to reconnect again, for example error 500 or 502.
Do you think I really need this reconnect function?
looks like you need it
I will reevaluate the need for this function and possibly remove it. I also want to ask if I use addEventListener for each event instead of using onmessage and handle all events in this function
Does it could be the cause of the error when using the reconnect func?
@AnhTuan-AiT , no, usage of onmessage in place of addEventListener should not make any difference
I am working on a web application using react and spring boot. To add live notification feature, I choosed server-sent-events with your library on the client. I set heartbeatTimeout to 120s and periodically send a heartbeat event every 40s to keep the connection open.
It works OK when I test it locally, but when I deploy the app it doesn't anymore. The connection is still open normally, the client still receives the heartbeat events in full and at the right time, but usually every 3 heartbeat events, the client gives an error: Error: No activity within N milliseconds. 96 characters received. Reconnecting.
I think the biggest difference between local environment and my deployment environment is that in local environment, client connects directly to backend, and in deployment environment, I use Nginx between them. But I still can't figure out which part of the deployment pattern is the reason of error.
Here is the code I use: React
Spring
Nginx
I really need support now