I’m trying to use WebSockets to communicate between an application using OCaml (Dream) on the server side and JSOO on the browser side. I have problems finding a library for this with JSOO.
brr - looks promising, but it looks like it does not implement all required functions.
vdom - seems to have WebSocket bindings.
Are there other libraries or solution for supporting WebSockets in JSOO?
At a more general level, I’m looking for a way for easy communication between the server and the browser, that also support push from the server. Something like Meteor pubsub or GraphQl. What is the best option for this in OCaml/JSOO?
Bascially, the client and server (dream) are discussing through websockets. When the server sends a long string, everything works well. When the client sends a long string, the string received by the server is garbage character at some point.
So if your app might send lots of data in a single message, be aware of this!
It’s probably me, not understanding how to use it properly. In JavaScript I would do something like:
var socket = new WebSocket("ws://" + window.location.host + "/ws");
socket.onopen = function () {
socket.send("start");
};
socket.onmessage = function (e) {
...
};
I’m able to connect to the server with Brr_io.Websocket.create, but I’m not able to use it. I was hoping to find similar functions as onopen and onmessage. At least the “addEventListener” method.
Is the “as_target” function used for setting up handlers? Do you have examples on how to use this, or a full example that shows how to set up and use the socket?
Sorry no sample code but yes you need to turn the websocket value into an event target and then use the DOM event API to setup your handlers (here’s a random example which you can accomodate with the right events).
The websocket specific events are here a few generic ones are in the DOM Event API here and the message event is here.
The websocket API predates JavaScript promises so it’s a bit inconvenient to use.
My current solution is to copy the required subset of my development environment (opam installed packages) over to the target nodes, in addition to the application. That was one of my main motivations for trying out Dream. It creates a single binary that can be copied and run. It would be a lot of help if the demo in ocsigen-start also could be built as a single static binary.
The second issue was that I had some problem with the application consuming memory.
I’m not sure if it is a bug, or I’m not using it correctly. However, for my current usage, it is not a big issue.
The last issue is editor support. When working with these modules, getting type information and suggestions is a lot of help. I see there is some improvement lately, so it might be partly solved. I have not tested the latest setup.
I saw these definitions, but could not figure out how to use them. I thought they are interface definitions, and that they requires an implementation. Do you know where I might find examples on how to use them?
You can have a look at the code @zoggy mentioned. That being said I personally don’t find jsoo’s way of interacting with JavaScript very usable (more on this here).
I have an HTML generation library for Dream that supports htmx out of the box so you can write HTML directly in your Dream server code. Adapting the first snippet from the htmx page:
open Dream_html
open HTML
let chatroom = div [Hx.ext "ws"; Hx.ws_connect "/chatroom"] [
div [id "notifications"] [];
div [id "chat_room"] [
...
];
form [id "form"; Hx.ws_send] [
input [name "chat_message"];
];
]
So you render all the pages on the server side and htmx takes care of the network interactions on the client side ie setting up the WS, sending and receiving messages, reconnecting on disconnect. So in practice you have to write little or no JavaScript code.