yawaramin / re-web

Experimental web framework for ReasonML & OCaml
https://yawaramin.github.io/re-web/re-web/index.html
MIT License
261 stars 8 forks source link

example that uses websockets #3

Closed tcoopman closed 4 years ago

tcoopman commented 4 years ago

It would be nice if there was an example that shows the usage of websockets. I'm willing to give this a try if you give me some pointers in how to do it.

yawaramin commented 4 years ago

Hi Thomas, great let me see if I can set you up :-)

ReWeb Manual section on WebSockets: https://yawaramin.github.io/re-web/re-web/Manual/Ch04_Responses/index.html#websocket-responses

Example of serving a WS from your router: https://github.com/yawaramin/re-web/blob/a599b33380a618bc2a890521f3aa6d4dd4968078/bin/Main.re#L247

Implementation of the above WS with more comments: https://github.com/yawaramin/re-web/blob/a599b33380a618bc2a890521f3aa6d4dd4968078/bin/Main.re#L148

Let me know if anything is unclear–I'd love to polish the docs.

tcoopman commented 4 years ago

Thanks for the response. The documentation and the example in Main.re is nice!

Reading through the documentation and the examples above, I tried to create the simplest possible websocket:

open ReWeb;

let ws = Response.of_websocket((_pull, push) =>
  "Hello, World!" |> push |> Lwt.return
);

let helloService = _request => Lwt.return(ws);
let server = _route => helloService;
let () = Server.serve(server);

this compiles fine, but when I surf to 8080 I get:

ReWeb.Server: listening on port 8080
ReWeb.Server: GET / 127.0.0.1
Fatal error: exception Failure("Headers.get_exn: \"sec-websocket-key\" not found")

Well reading into this, that seems obvious, as I didn't request a websocket, but I wouldn't expect that the server would crash here.

To be clear, I did get it to work after that, so my original issue is solved, but not sure if the issue above this warrants a different issue.

yawaramin commented 4 years ago

Ah, interesting! I'm going to treat that as a new issue: #4