Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Socket connection with Perl (server side) and HTML web page.

by TorontoJim (Beadle)
on Feb 22, 2023 at 16:13 UTC ( [id://11150532]=perlquestion: print w/replies, xml ) Need Help??

TorontoJim has asked for the wisdom of the Perl Monks concerning the following question:

I'm researching how to implement a Socket connection with a web page. All the examples I've found seem to be NOT for a web page as the clients are all in Perl.

My backend is vanilla Perl. The client will be an HTML page. I need a real time exchange between the two so multiple users (max 4 or 5) can have the web page open and make updates that each will see right away.

I see lots of examples on of the server script which appears straightforward. I've found nothing that shows the client side (the HTML page in the browser) that could connect with the Perl socket server script.

So, does anyone have an example (both sides) that why would be willing to share? I need to see what to do on the web page side to work with the server side.

Pretty please and thank you for your time.

  • Comment on Socket connection with Perl (server side) and HTML web page.

Replies are listed 'Best First'.
Re: Socket connection with Perl (server side) and HTML web page.
by Fletch (Bishop) on Feb 22, 2023 at 16:20 UTC

    Mojolicious has a sample web socket in the cookbook guide.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Socket connection with Perl (server side) and HTML web page.
by NERDVANA (Deacon) on Feb 24, 2023 at 07:28 UTC
Re: Socket connection with Perl (server side) and HTML web page.
by atcroft (Abbot) on Feb 23, 2023 at 14:44 UTC
Re: Socket connection with Perl (server side) and HTML web page.
by cavac (Parson) on Feb 27, 2023 at 18:22 UTC

    Socket? Do you mean a websocket? See Connecting OBS to my home automation network on how to write a Perl client.

    As for HTML: Browsers only allow Javascript. See Javascript Websocket

    Well, technically, you could fiddle with something like emscripten to cross-compile any C program (inkluding Perl) to Javascript. That's a MAJOR undertaking but it's technically possible. That is how the people at archive.org have cross-compiled the DOSBox emulator so that you can play old DOS games on their website. You still wouldn't be able to use raw network sockets.

    I've done a fair bit with Websockets, see BaseWebSocket.pm and it's corresponding HTML template basewebsocket.tt. All this is pretty tied to my own framework, but if you need any help doing something similar in yours, feel free to ask.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re: Socket connection with Perl (server side) and HTML web page.
by hippo (Bishop) on Feb 23, 2023 at 16:53 UTC
    I've found nothing that shows the client side (the HTML page in the browser) that could connect with the Perl socket server script.

    Was Fletch's suggestion what you were looking for or did you want to write the client side in WebPerl rather than Javascript?


    🦛

Re: Socket connection with Perl (server side) and HTML web page.
by sectokia (Pilgrim) on Feb 23, 2023 at 23:22 UTC

    A web browser client can only run javascript given by the server in the HTML severed. Javascript does not allow raw access to sockets, it only allows certain HTTP use and certain protocols like websockets.

    HTTP is limited to a client request and a server response (a server cannot just send to a client - the client has to request it i.e. ‘fetch’ .

    To do a HTTP fetch in javascript use the fetch api (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) which will be a http request to your server.

    If you need the server to be able to push data to the client - you will need to use web sockets (https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). In this case the server would server over http javascript that tells the client to open a websocket to be able to receive from server. on the perl side you would then need a websocket server (https://metacpan.org/pod/Net::WebSocket::Server) in addition to the http server.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11150532]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found