Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2022-08

pas2js and server sent events

16 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

Hello,

I see that pas2js has support for websockets, but I cannot find if it also supports server sent events.

A couple of years ago I implemented an application with sse where I used fpWeb and plain html+javascript in the browser.

Now I need to implement a more complex application and I'd like to use pas2js, if it supports sse I can reuse the old server class, if it doesn't I'll have to reimplement it as a websocket server.

Bye

Luca

Reply by email

Mattias Gaertner
to Luca Olivetti
Hello, I see that pas2js has support for websockets, but I cannot find if it also supports server sent events.

A websocket is bidirectional.

  WebSocket:=TJSWebSocket.new(URL);
  ;
  WebSocket.binaryType := 'arraybuffer';
function TWebapp.OnWebSocketMessage(Event: TEventListenerEvent
  ): boolean;
var
  MsgEvent: TJSMessageEvent;
  ArrBuffer: TJSArrayBuffer;
begin
  Result:=true;
  MsgEvent:=TJSMessageEvent(Event);
  ArrBuffer:=TJSArrayBuffer(MsgEvent.Data);
  ...
end;
Show quoted text 6 lines
A couple of years ago I implemented an application with sse where I used fpWeb and plain html+javascript in the browser. Now I need to implement a more complex application and I'd like to use pas2js, if it supports sse I can reuse the old server class, if it doesn't I'll have to reimplement it as a websocket server.

Mattias

Reply by email

Michael Van Canneyt
to Luca Olivetti: I see that pas2js has support for websockets, but I cannot find if it also supports…
Luca Olivetti wrote 11 lines
Hello, I see that pas2js has support for websockets, but I cannot find if it also supports server sent events. A couple of years ago I implemented an application with sse where I used fpWeb and plain html+javascript in the browser. Now I need to implement a more complex application and I'd like to use pas2js, if it supports sse I can reuse the old server class, if it doesn't I'll have to reimplement it as a websocket server.

If you could do it with Javascript, you are able to do it with pas2js.

I added the definition of EventSource class to the weborworker unit, so you can work in a type-safe manner.

Michael.

Reply by email

Luca Olivetti
to Mattias Gaertner: On Fri, 26 Aug 2022 07:39:40 +0000 (UTC) A websocket is bidirectional.…

El 26/8/22 a les 11:25, Mattias Gaertner ha escrit:

Show quoted text 9 lines
On Fri, 26 Aug 2022 07:39:40 +0000 (UTC) Luca Olivetti wrote: > Hello, > > I see that pas2js has support for websockets, but I cannot find if it > also supports server sent events. A websocket is bidirectional.

Yes, I know, but I'm lazy ;-) and if I can reuse the server class I already implemented for sse I would be happy. Though maybe it wouldn't be too much work

WebSocket:=TJSWebSocket.new(URL); ; WebSocket.binaryType := 'arraybuffer';

Is this on the client or on the server? I couldn't find a freepascal TJSWebSocket class on duckduckgo. I could find
https://github.com/Warfley/LazWebsockets but the class is called TWebSocketServer.

Bye

Luca

Reply by email

Mattias Gaertner
to Luca Olivetti
Show quoted text 20 lines
El 26/8/22 a les 11:25, Mattias Gaertner ha escrit: > On Fri, 26 Aug 2022 07:39:40 +0000 (UTC) > Luca Olivetti wrote: > >> Hello, >> >> I see that pas2js has support for websockets, but I cannot find if >> it also supports server sent events. > > A websocket is bidirectional. Yes, I know, but I'm lazy ;-) and if I can reuse the server class I already implemented for sse I would be happy. Though maybe it wouldn't be too much work > > WebSocket:=TJSWebSocket.new(URL); > ; > WebSocket.binaryType := 'arraybuffer'; Is this on the client or on the server?

client - pas2js

Do you want to use pas2js on the server side?

Mattias

Reply by email

Luca Olivetti
to Mattias Gaertner

El 26/8/22 a les 13:33, Mattias Gaertner ha escrit:

Show quoted text 5 lines
> Is this on the client or on the server? client - pas2js Do you want to use pas2js on the server side?

No, I want to use it only on the client side, hence my question about server sent events (I already have a server class for that).

Bye

Luca

Reply by email

Luca Olivetti
to Michael Van Canneyt: If you could do it with Javascript, you are able to do it with pas2js. I added the…

El 26/8/22 a les 11:32, Michael Van Canneyt ha escrit:

I added the definition of EventSource class to the weborworker unit, so you can work in a type-safe manner.

Thank you.
In case I go the websocket route I'll have to use a different server on a different port than fpWeb, right?
In that case would the client have problems in connecting to a different port? (since nowadays browsers are more restrictive and rightly so).

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 10 lines
El 26/8/22 a les 11:32, Michael Van Canneyt ha escrit: > I added the definition of EventSource class to the weborworker unit, so you > can work in a type-safe manner. Thank you. In case I go the websocket route I'll have to use a different server on a different port than fpWeb, right?

No.

The HTTP implementation of FPC understands the upgrade request and will switch to websocket, so you can use the same port.

You must use trunk for this, however.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 26/8/22 a les 13:58, Michael Van Canneyt ha escrit:

Show quoted text 22 lines
On Fri, 26 Aug 2022, Luca Olivetti wrote: > El 26/8/22 a les 11:32, Michael Van Canneyt ha escrit: > > >> I added the definition of EventSource class to the weborworker unit, >> so you >> can work in a type-safe manner. > > > Thank you. > In case I go the websocket route I'll have to use a different server > on a different port than fpWeb, right? No. The HTTP implementation of FPC understands the upgrade request and will switch to websocket, so you can use the same port. You must use trunk for this, however.

Mmmh, in case I don't want to use trunk, will the browser impose some restriction to connect to a different port?

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 26 lines
El 26/8/22 a les 13:58, Michael Van Canneyt ha escrit: > > > On Fri, 26 Aug 2022, Luca Olivetti wrote: > >> El 26/8/22 a les 11:32, Michael Van Canneyt ha escrit: >> >> >>> I added the definition of EventSource class to the weborworker unit, so >>> you >>> can work in a type-safe manner. >> >> >> Thank you. >> In case I go the websocket route I'll have to use a different server on a >> different port than fpWeb, right? > > No. > > The HTTP implementation of FPC understands the upgrade request and will > switch to websocket, so you can use the same port. > > You must use trunk for this, however. Mmmh, in case I don't want to use trunk, will the browser impose some restriction to connect to a different port?

It can be that CORS interferes, but since it is GET only, I would think not.

In each case, you'll need trunk if you want websockets: websockets are only available in trunk.

Michael.

Reply by email

Luca Olivetti
to Mattias Gaertner

El 26/8/22 a les 17:34, Mattias Gaertner ha escrit:

Show quoted text 8 lines
On Fri, 26 Aug 2022 15:11:42 +0200 (CEST) Michael Van Canneyt wrote: > [...] > In each case, you'll need trunk if you want websockets: > websockets are only available in trunk. Note that the units can be compiled with fpc 3.2.2 just fine.

I tried but I failed:

-fcl-web depends on fcl-hash which is not available in 3.2.2 -fcl-hash depends on basenenc in fcl-base
-I copied basenenc.pp to fcl-hash and could compile it -then fcl-web uses fpthreadpool.pp, copied it from trunk -then fcl-web fails to compile:

fphttpserver.pp(1068,39) Error: identifier idents no member "CanRead" fphttpserver.pp(1634) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 22 lines
El 26/8/22 a les 17:34, Mattias Gaertner ha escrit: > On Fri, 26 Aug 2022 15:11:42 +0200 (CEST) > Michael Van Canneyt wrote: > >> [...] >> In each case, you'll need trunk if you want websockets: >> websockets are only available in trunk. > > Note that the units can be compiled with fpc 3.2.2 just fine. I tried but I failed: -fcl-web depends on fcl-hash which is not available in 3.2.2 -fcl-hash depends on basenenc in fcl-base -I copied basenenc.pp to fcl-hash and could compile it -then fcl-web uses fpthreadpool.pp, copied it from trunk -then fcl-web fails to compile: fphttpserver.pp(1068,39) Error: identifier idents no member "CanRead" fphttpserver.pp(1634) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted

You must also copy ssockets from fcl-net

Michael.

Reply by email

Mattias Gaertner
to Luca Olivetti: El 26/8/22 a les 17:34, Mattias Gaertner ha escrit: I tried but I failed: -fcl-web…
Show quoted text 22 lines
El 26/8/22 a les 17:34, Mattias Gaertner ha escrit: > On Fri, 26 Aug 2022 15:11:42 +0200 (CEST) > Michael Van Canneyt wrote: > >> [...] >> In each case, you'll need trunk if you want websockets: >> websockets are only available in trunk. > > Note that the units can be compiled with fpc 3.2.2 just fine. I tried but I failed: -fcl-web depends on fcl-hash which is not available in 3.2.2 -fcl-hash depends on basenenc in fcl-base -I copied basenenc.pp to fcl-hash and could compile it -then fcl-web uses fpthreadpool.pp, copied it from trunk -then fcl-web fails to compile: fphttpserver.pp(1068,39) Error: identifier idents no member "CanRead" fphttpserver.pp(1634) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted

Copy these units to a folder

fpcustwsserver.pp
fphttpserver.pp
fpthreadpool.pp
fpwebsocketclient.pp
fpwebsocket.pp
fpwebsocketserver.pp
httpdefs.pp
httpprotocol.pp
httproute.pp
sslsockets.pp
ssockets.pp
wsupgrader.pp

Compile wsupgrader.pp

Mattias

Reply by email

Luca Olivetti
to Mattias Gaertner

El 26/8/22 a 17:59, Mattias Gaertner ha escrit:

Show quoted text 16 lines
Copy these units to a folder fpcustwsserver.pp fphttpserver.pp fpthreadpool.pp fpwebsocketclient.pp fpwebsocket.pp fpwebsocketserver.pp httpdefs.pp httpprotocol.pp httproute.pp sslsockets.pp ssockets.pp wsupgrader.pp Compile wsupgrader.pp

Thank you!

with those files I managed to compile and test the example websocket client and server.
To compile the httpupgrader example I had to add

fpwebfile.pp
fphttp.pp
httpjson.pp

I put them all in an lpk so I can experiment further.

Bye

Luca

Reply by email