How to use WebSocket.webSocket class in ocsigen?

Hello everyone!
I tried to read a json file from disk and send it as a string to the client in ocsigen. Because after getting longer than 100 lines in my opinion every code file becomes a hell of mess to understand I decided to separate the client code from main.eliom file and put it into and in main.eliom I created a function like:

let read_json path =
  let json = Yojson.Basic.from_file path in
  Yojson.Basic.Util.to_string json

and when I wanted to call it in like: ~%read_json "./static/js/data.json" I got the error:

Error: Unbound value ~%
Makefile:208: recipe for target '_client/client.cmo' failed

Then I changed my mind to use websocket for rpc and I found WebSocket.webSocket class in js_of_ocaml without any example of how to use it I tried in client and server code this line of code:

let ws_obj = (new WebSockets.webSocket)#url "ws://localhost:8080/json"

in fact without being able to get any results but the error:

Error: Unbound class WebSockets.webSocket

how to make a simple websocket server and use it on client side in two separate files?
any help would be very appreciated. Thanks in advance.

I won’t be of very much help on the compilation errors but the websocket module is more or less a straight translation from the WebSocket API:

As the original author of this module I’m really sorry about the lack of documentation.

For calling a server-side function from a client-side code in a separate file you can do this for this example in main.eliom file add:

let read_json path =
  let json = Yojson.Basic.from_file path in
  Lwt.return (Yojson.Basic.to_string json)

let%client read_json = ~%(Eliom_client.server_function [%derive.json : string] read_json)

then you can call read_json in file as:

open Main
let%lwt jsn = read_json "file.json"

but please help me with WebSockets.webSocket, Thanks!

Mmh I just noticed that you were talking about server-side websockets as well. The WebSockets module is client side only (everything in js_of_ocaml is).

Rereading was a bit helpful in fact. The Error: Unbound class WebSockets.webSocket comes from the fact that WebSockets.webSocket is not a class but a JS constructor and cannot be instantiate but the new%js constr e1 ... en construct should be used instead.
See for more “technical” documentation about the syntax extension.

Thank you! I will try to write a websocket server with other capabilities of OCaml. Thanks for your help of how to use WebSocket.webSocket by new%js and Happy New Year!

I believe this library is already doing websockets on server-side:
It is available via opam by doing: opam install websocket or opam install websocket-lwt

Happy new year to you too.

Did anyone manager to use the ocaml-websocket library? I tried and got header problems, possibly because of no SSL setup on my localhost.

I used it successfully a couple years ago. The code is probably a mess, but you can see the server side of things here, and (though it doesn’t use the OCaml library) it’s accessed on the browser side here.