#1 2019-10-31 20:05:44

Yusuke26
Member
Registered: 2019-10-08
Posts: 2

React with WebSockets on Linux

Hi,
I can't find the way to enable a WebSocket communication between React and a mORMot server, can someone give me an example please?

Offline

#2 2019-11-01 07:10:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: React with WebSockets on Linux

See in the '31 - WebSockets' folder the Project31SimpleEchoServer.dpr sample.
It is a low-level WebSockets server, and there is a JavaScript client.

We use such a dedicated "protocol" class for WebSockets protocol with React/Angular clients.
It is easier/more direct to work with than interface-based services, for a JS client.

Offline

#3 2019-11-01 22:09:48

Yusuke26
Member
Registered: 2019-10-08
Posts: 2

Re: React with WebSockets on Linux

Thank you very much Mr. Bouchez, it now works like a charm!

Offline

#4 2019-11-01 22:20:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: React with WebSockets on Linux

smile

Offline

#5 2020-03-24 14:37:07

aroxing
Member
Registered: 2016-04-24
Posts: 8

Re: React with WebSockets on Linux

ab wrote:

See in the '31 - WebSockets' folder the Project31SimpleEchoServer.dpr sample.
It is a low-level WebSockets server, and there is a JavaScript client.

We use such a dedicated "protocol" class for WebSockets protocol with React/Angular clients.
It is easier/more direct to work with than interface-based services, for a JS client.

But is it possible to implement interfaced-based services for a JS client?
I'm asking because it's a very powerful idea to systemize all commands/events between distributed software parts. It's not cool to neglect such a good tool like interfaced-based services doing instead low level Websockets interactions (parsing, formatting etc).

Thanks.

Offline

#6 2020-03-24 19:51:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: React with WebSockets on Linux

Interface-base services are over HTTP/REST by now for JavaScript.
They are not supported yet with JavaScript.
You can create asynch interface-based services in Delphi/FPC and consume them from Delphi/FPC, but you will have to write all the low-level JSON code by hand in JS if you want to have a JS client.

Offline

#7 2020-03-25 13:22:34

aroxing
Member
Registered: 2016-04-24
Posts: 8

Re: React with WebSockets on Linux

ab wrote:

Interface-base services are over HTTP/REST by now for JavaScript.
They are not supported yet with JavaScript.
You can create asynch interface-based services in Delphi/FPC and consume them from Delphi/FPC, but you will have to write all the low-level JSON code by hand in JS if you want to have a JS client.

Ab, thanks for the answer.
I have the architecture the same you mentioned: 1) Delphi 7 server, 2) Delphi 7 Client, 3) Web (HTML5, JS) client. I'd like to use a unified data communication protocol to link them. I'm looking for Websockets-oriented classes in mORMot Framework to implement iterface-based approach.
As far as I know a communication between Delphi Server and Delphi Client could be done as like in Project31LongWorkServer and Project31LongWorkClient:
-- Client: TSQLHttpClientWebsockets;
-- Server: TSQLHttpServer.

But in the Project31SimpleEchoServer it is used TWebSocketServer which is perfectly works with JS Client but is not compatible with TSQLHttpClientWebsockets.

Sorry for the long description but could you please suggest some classes that I can use to combine Delphi Server, Delphi Client and JS Client based on interface approach?

Thanks,
Andrii.

Offline

#8 2020-03-25 16:01:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: React with WebSockets on Linux

The problem is on the JS side: there is currently no easy support of our WebSocket JSON frame format.
You have to make the decoding/encoding at hand.

Offline

#9 2020-03-25 18:20:33

aroxing
Member
Registered: 2016-04-24
Posts: 8

Re: React with WebSockets on Linux

ab wrote:

The problem is on the JS side: there is currently no easy support of our WebSocket JSON frame format.
You have to make the decoding/encoding at hand.

Thanks.

I have modified code a little changing binary protocol to json one:

Project31LongWorkServer.dpr
  HttpServer.WebSocketsEnable(Server,'', True, False);

Project31LongWorkClient.dpr
  Client.WebSocketsUpgrade(PROJECT31_TRANSMISSION_KEY, True, False);

Project31SimpleEchoServer.html
  socket = new WebSocket("ws://localhost:8888/root", "synopsebin");

After that JS code connected to WS Server successfully.

So my last question here is could share some references where I can find details of JSON protocol? I'd like to try implementing it on JS side.

Thanks again for the perfect framework and your support.

Best regards,
Andrii.

Offline

#10 2020-03-26 00:28:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: React with WebSockets on Linux

There is no documentation about the JSON protocol itself yet.

The class implementing it is TWebSocketProtocolJSON.
See https://synopse.info/files/html/api-1.1 … OTOCOLJSON

You will find the JSON layout in the source code.
It is just a few lines.

But the higher level protocol, i.e. REST emulation over WebSockets, is implemented in TWebSocketProtocolRest.
It is the common ancestor to both TWebSocketProtocolBinary and TWebSocketProtocolJSON.
The REST emulation over WebSockets is a bit tricky, since it needs to take track of the request/answers over the wire.

Offline

Board footer

Powered by FluxBB