Recent

Author Topic: Client for Server Side Events (SSE), listen push notifications  (Read 1081 times)

noszone

  • New Member
  • *
  • Posts: 27
Client for Server Side Events (SSE), listen push notifications
« 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.

loaded

  • Sr. Member
  • ****
  • Posts: 308
Re: Client for Server Side Events (SSE), listen push notifications
« Reply #1 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.
« Last Edit: September 04, 2021, 07:10:16 am by loaded »
Lazarus V2.0.10 32 Bit + Intel I5 8250U + 12GB RAM + Win 10 Home 64 Bit = red pill
Morpheus: "You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes."

noszone

  • New Member
  • *
  • Posts: 27
Re: Client for Server Side Events (SSE), listen push notifications
« Reply #2 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.

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: Client for Server Side Events (SSE), listen push notifications
« Reply #3 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.
« Last Edit: September 06, 2021, 03:38:05 pm by Warfley »

noszone

  • New Member
  • *
  • Posts: 27
Re: Client for Server Side Events (SSE), listen push notifications
« Reply #4 on: September 07, 2021, 12:50:21 pm »
Thanks All,

We succesfully used a 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