Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example that uses websockets #3

Closed
tcoopman opened this issue Jan 17, 2020 · 3 comments
Closed

example that uses websockets #3

tcoopman opened this issue Jan 17, 2020 · 3 comments

Comments

@tcoopman
Copy link
Contributor

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
Copy link
Owner

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:

// Example of serving a WebSocket

Implementation of the above WS with more comments:

/** [getEchoWS(request)] is a service that handles a WebSocket

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

@tcoopman
Copy link
Contributor Author

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants