Lazarus

Programming => Networking and Web Programming => Topic started by: noszone on September 03, 2021, 01:00:43 pm

Title: Client for Server Side Events (SSE), listen push notifications
Post by: noszone on September 03, 2021, 01:00:43 pm
Does anybody knows hot to listen for SSE in Lazarus? With Indy (latest version) it's possible, but sometimes (1 per 100 records) it gives an error (Connection closed gracefully) in modal window. I am looking for another way to listen for server side events. What about FPHTTPClient from fpWeb?

I am trying to read a json from http page which constantly updates with SSE events. In case with IndyHttp client I've used OnChunkReceived. Please advise if any suggestions.
Title: Re: Client for Server Side Events (SSE), listen push notifications
Post by: loaded on September 03, 2021, 10:23:10 pm
First of all, Remy Lebeau, who is an Indy software developer, is active on this page. Chances are, if he sees the notification, he can fix your problem very easily.
The alternative that I will offer on the subject is;
You can try CEF4Delphi.  I recommend you to investigate ResourceResponse and ProcessMessageReceived methods.There are very detailed demos in the installation package.
Along with these;
If you use javascript you will reach your goal much faster.
Title: Re: Client for Server Side Events (SSE), listen push notifications
Post by: noszone on September 06, 2021, 05:28:13 am
Thanks for heads up, also for useful tips. The project is fully on Lazarus and can't use a JS.
Title: Re: Client for Server Side Events (SSE), listen push notifications
Post by: Warfley on September 06, 2021, 03:32:36 pm
AFAIK in Indy exceptions are not necessarily errors.  An exception is just that, an exception. An example for this I guess is that in FTP protocol you can prematurely close the control connection while data is still transmitted. This will cause an exception, even though it is not an error, just uncommon (but still standardized) behavior.

In fact the "Connection closed gracefully" exception is AFAIK indys way of signaling that the connection was closed by the other side. It is not an error in the strict sense, it is just an information for your application that the other side has decided to close the transmission.
The reason for this could be that the other side might want to avoid long lasting connections and requires you to reconnect every few minutes or so. This can be a form of load balancing.

When you get this exception, just reconnect and try again. HTTP uses connection keep-alive to reuse one connection for multiple requests. As HTTP is stateless, the server or client can terminate this connection after each request, e.g. if they want to save resources or think you or they are done (e.g. when the last element of a website has been requested). But you should easiely be able to reconnect with the next request and continue.
Title: Re: Client for Server Side Events (SSE), listen push notifications
Post by: noszone on September 07, 2021, 12:50:21 pm
Thanks All,

We succesfully used a https://wiki.lazarus.freepascal.org/fphttpclient (https://wiki.lazarus.freepascal.org/fphttpclient). This component in thread can read SSE with no problems at all. Paired with TStream we can read all data.
TinyPortal © 2005-2018