When a server gives its events an id: field, TsgcSSEClient stores the last one in the LastEventId property and sends it in the Last-Event-ID request header every time it reconnects. The server reads that header and sends only the events the client has not received yet, so a dropped connection does not lose messages.
Inside one session this works without any code. To resume after the application restarts, save LastEventId when the client stops and restore it before calling Open. LastEventId keeps its value after Close, and a value set before Open is sent with the first request too.
Restore the id and open the stream
var
oFile: TStringList;
begin
oSSE.URL := 'https://www.example.com/events';
oSSE.OnClose := OnSSEClose;
if FileExists('sse_last_id.txt') then
begin
oFile := TStringList.Create;
Try
oFile.LoadFromFile('sse_last_id.txt');
oSSE.LastEventId := Trim(oFile.Text);
Finally
oFile.Free;
End;
end;
oSSE.Open;
end;
Close the stream and save the id
var
oFile: TStringList;
begin
oSSE.Close;
oFile := TStringList.Create;
Try
oFile.Text := oSSE.LastEventId;
oFile.SaveToFile('sse_last_id.txt');
Finally
oFile.Free;
End;
end;
Resuming only works when the server supports it: it must send an id: field with the events and read the Last-Event-ID header of the request.