reason-native-web / morph

A webframework for Reason and OCaml
https://reason-native-web.github.io/morph/
MIT License
139 stars 7 forks source link

Support websockets #27

Open ulrikstrid opened 4 years ago

ulrikstrid commented 4 years ago

We need a way to support websockets.

For httpaf we can use this to upgrade the connection: https://github.com/anmonteiro/httpaf/blob/fork/lib/httpaf.mli#L640. For h2 we can just take the TCP connection and do the same thing, if we want to support it at all.

The biggest problem is how to build the API. My current thought is that we should add a function to the request to get hold of a socket (basically a way to send and receive data, maybe via Lwt_stream.t). And then to close the connection we return the response, but that might feel weird since we've actually already responded. Another solution would be to have a Socket body that does the same thing but then the term body is overloaded.

yawaramin commented 4 years ago

How about something like:

// Echo websocket
Morph.Response.webSocket("/path", (send, recv) =>
  Lwt_stream.iter(recv, string => {
    print_endline({|Received data from client: "|} ++ string ++ {|"|});
    send(if (string == "close") None else Some(string));
  }
);

That's a really simplified version but I think it gets the idea across.