Websocket Server Implementations for Delphi

WebSocket client implementations for Delphi

Here is my open source library:
https://github.com/andremussche/DelphiWebsockets

Delphi server with JavaScript client

There are some WebSocket client and server implementations available (see WebSocket server implementations for Delphi).

By design, client-side JavaScript (in the browser) can use the WebSocket protocol to communicate with the server. The Delphi WebSocket server implementations then can handle the requests and push data back to the client just like any other WebSocket server library. However, I do not know anything about the code quality or Delphi version compatibility of all these libraries so some additional research is required.

Regarding the Demo: most libraries surely include some demo HTML with JavaScript / WebSocket communication. Simply download it and open it in your favorite text editor ;)

p.s. as I can see socket.io not only supports WebSocket but also long polling so basically you can use any HTTP server library for Delphi to write the server side logic. See:

  • How can I update HTML pages dynamically with Indy HTTP server using jQuery and "Long Polling"?
  • How to: update HTML pages dynamically using jQuery and “Long Polling”

Delphi - Websockets and uniGUI

1: yes, make a "single page app" (static html + js) and communicate with server using xml or json.
At least with indy 10 it is quite straight forward to implement static file serving (response.contentstream := tfilestream.create(sfilename) or something like that)

2: yes, if you search for it on google you will find some :)
WebSocket server implementations for Delphi

btw, I'm busy with delphi socket.io implementation, which makes it easier to use websockets instead of plain low level tcp-like websockets itself

How to trigger an event from a web server to client en Delphi?

WebSocket (a HTTP upgrade) is a light-weight option and also available for Delphi clients:

WebSocket is designed to be implemented in web browsers and web
servers, but it can be used by any client or server application. The
WebSocket Protocol is an independent TCP-based protocol.

See WebSocket client implementations for Delphi

RabbitMQ and other solutions are useful for example if the client could be offline while the server wants to send the message. The message broker provides a store where the message will be waiting for the client, even if the server restarts. Also a message broker will reduce load on the HTTP server.

HTTP continuous packeted stream with Indy

While using TCP stream was an option, in the end I went with original solution of writing custom TIdIOHandlerStack descendant.

The motivation was that with TIdHTTP I know what doesn't work and only need to fix that, while switching to lower level TCP means new problems can arise.

Here's the code that I'm using, and I'm going to discuss the key points here.

New TIdStreamIoHandler has to inherit from TIdIOHandlerStack.

Two functions need to be rewritten: ReadBytes and ReadStream:

function TryReadBytes(var VBuffer: TIdBytes; AByteCount: Integer;
AAppend: Boolean = True): integer; virtual;
procedure ReadStream(AStream: TStream; AByteCount: TIdStreamSize = -1;
AReadUntilDisconnect: Boolean = False); override;

Both are modified Indy functions which can be found in IdIOHandler.TIdIOHandler. In ReadBytes the while clause has to be replaced with a singe ReadFromSource() request, so that TryReadBytes returns after reading up to AByteCount bytes in one go.

Based on this, ReadStream has to handle all combinations of AByteCount (>0, <0) and ReadUntilDisconnect (true, false) to cyclically read and then write to stream chunks of data arriving from the socket.

Note that ReadStream need not terminate prematurely even in this stream version if only part of the requested data is available in the socket. It just has to write that part to the stream instantly instead of caching it in FInputBuffer, then block and wait for the next part of data.



Related Topics



Leave a reply



Submit