# How to use WebSocket.webSocket class in ocsigen?

**URL:** <https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385>\
**Category:** Learning\
**Tags:** ocsigen\
**Created:** [January 3, 2018, 11:40am UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385 "2018-01-03T11:40:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tidou](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@tidou](https://discuss.ocaml.org/u/tidou)\
**Post date:** [January 3, 2018, 11:40am UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/1 "2018-01-03T11:40:27Z")

</div>

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 `client.ml` and in main.eliom I created a function like:

```auto
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 `client.ml` like: `~%read_json "./static/js/data.json"` I got the error:

```auto
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:

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

```

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![jpdeplaix](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jpdeplaix/32/780_2.png) [@jpdeplaix](https://discuss.ocaml.org/u/jpdeplaix)\
**Post date:** [January 4, 2018, 12:08pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/2 "2018-01-04T12:08:12Z")

</div>

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: [https://developer.mozilla.org/en-US/docs/Web/API/WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)

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

---

<div class="post-metadata">

**Author:** ![tidou](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@tidou](https://discuss.ocaml.org/u/tidou)\
**Post date:** [January 4, 2018, 12:15pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/3 "2018-01-04T12:15:43Z")

</div>

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:

```auto
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 `client.ml` file as:

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

```

but please help me with WebSockets.webSocket, Thanks!

---

<div class="post-metadata">

**Author:** ![jpdeplaix](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jpdeplaix/32/780_2.png) [@jpdeplaix](https://discuss.ocaml.org/u/jpdeplaix)\
**Post date:** [January 4, 2018, 12:18pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/4 "2018-01-04T12:18:57Z")

</div>

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 [http://ocsigen.org/js\_of\_ocaml/2.8.4/api/Ppx\_js](http://ocsigen.org/js_of_ocaml/2.8.4/api/Ppx_js) for more “technical” documentation about the syntax extension.

---

<div class="post-metadata">

**Author:** ![tidou](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@tidou](https://discuss.ocaml.org/u/tidou)\
**Post date:** [January 4, 2018, 12:23pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/5 "2018-01-04T12:23:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jpdeplaix](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jpdeplaix/32/780_2.png) [@jpdeplaix](https://discuss.ocaml.org/u/jpdeplaix)\
**Post date:** [January 4, 2018, 12:25pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/6 "2018-01-04T12:25:33Z")

</div>

I believe this library is already doing websockets on server-side: [https://github.com/vbmithr/ocaml-websocket](https://github.com/vbmithr/ocaml-websocket)  
It is available via opam by doing: `opam install websocket` or `opam install websocket-lwt`

Happy new year to you too.

---

<div class="post-metadata">

**Author:** ![olleharstedt](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@olleharstedt](https://discuss.ocaml.org/u/olleharstedt)\
**Post date:** [May 24, 2020, 10:43pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/7 "2020-05-24T22:43:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Levi\_Roth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/levi_roth/32/2268_2.png) [@Levi\_Roth](https://discuss.ocaml.org/u/Levi_Roth)\
**Post date:** [May 24, 2020, 11:57pm UTC](https://discuss.ocaml.org/t/how-to-use-websocket-websocket-class-in-ocsigen/1385/8 "2020-05-24T23:57:54Z")

</div>

I used it successfully a couple years ago. The code is probably a mess, but you can see the server side of things [here](https://github.com/leviroth/backgammon/blob/52386adad48e98105058be1f9753f9acfc8673ef/server/main.ml), and (though it doesn’t use the OCaml library) it’s accessed on the browser side [here](https://github.com/leviroth/backgammon/blob/master/client/main.ml).
