Skip to content

Conversation

patricoferris
Copy link

This PR adds direct-style websockets using eio and cohttp-eio (yet unreleased, but it has been merged in the main branch of mirage/ocaml-cohttp). It is WIP and probably will change, but I thought I would open this PR in case I don't have time to come back to it later.

cc: @bikallem @talex5 who might be able to comment on the core IO implementation a little :))

@patricoferris patricoferris changed the title Eio Direct-style websockets with Eio Sep 2, 2022
Copy link

@talex5 talex5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about web-sockets, but the Eio stuff looks reasonable to me. I suspect you don't need most of the Buf_write.flush calls, though. The buffer gets flushed automatically when you block; flush just makes the caller wait until the data has finished flushing but doesn't speed it up (any more than calling Fiber.yield () would, anyway).

@ryanslade
Copy link

Is there anything holding back this PR for merge? Anything I can perhaps do to help push it over the line?

@patricoferris
Copy link
Author

Is there anything holding back this PR for merge?

Thanks for the interest and offer for help @ryanslade. I would say this PR is blocked on landing a proper release of cohttp-eio. Right now, it is an alpha release and constrains us to eio.0.6 which means we also don't have access to Eio.Buf_read.BE for example. There is some experimentation on what a good HTTP library might look like, see mirage/ocaml-cohttp#969 but I think this implies it's going to be a while before we see a full release of cohttp-eio.

@ushitora-anqou
Copy link
Contributor

Hi. Cohttp 6.0.0~beta1 is out and it has Cohttp_eio.Private.IO module, which implements Cohttp.S.IO. As the Lwt version of ocaml-websocket does, we can feed it to Websocket.Make to get the necessary functions for WS server and client implementations. Personally, I needed an Eio version of a WS client implementation and tried this strategy, which worked just fine. I have left the code here for anyone who needs it.

@Kakadu
Copy link

Kakadu commented Aug 22, 2024

I tried to build this PR with cohttp-eio.6.0.0~alpha2 but stuck with compilation error.

File "ocaml-websocket/eio/websocket_eio.ml", line 61, characters 23-29:
61 |       Cohttp_eio.Body.(Custom f) ) in
                            ^^^^^^
Error: Unbound constructor Custom

I would say this PR is blocked on landing a proper release of cohttp-eio.

@patricoferris have things become better in last year?

@patricoferris
Copy link
Author

Quickly bump to work with cohttp-eio.6.0.0~beta2 should make this work with the beta2 branch of cohttp-eio -- I don't have time to work on this in any detail so please do take the code and run with it :)) Good luck !

@sporkl
Copy link

sporkl commented Mar 11, 2025

Are there any updates about this? It looks like cohttp-eio is out of beta.

Depending on how much time I end up having over the next couple weeks, I might be able to try to spend some time with it, if that could be useful.

Copy link
Collaborator

@copy copy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this and it works well.

I left a few suggestions. Otherwise, this looks good to merge to me.


let write_frame_to_buf ~mode buf fr =
let open Frame in
let content = Bytes.unsafe_of_string fr.content in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is later written to (the xor call below), so it should use Bytes.to_string, at least in the client case.

(public_name websocket-async)
(modules websocket_async)
(optional)
(libraries websocket logs-async cohttp-async))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this change?

match read_exactly ic payload_len with
| None -> proto_error "could not read payload (len=%d)" payload_len
| Some payload ->
let payload = Bytes.unsafe_of_string payload in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Bytes.unsafe_of_string is safe here.

@@ -0,0 +1 @@
(vendored_dirs ocaml-cohttp)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed any more.

process incoming websocket frames, and returns ([response_action],
[push_frame]) where [response_action] is used to produce a
{!Cohttp_lwt.Server.t} and [push_frame] is used to send websocket
frames to the client. *)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise this matches the Lwt API, but maybe this is an opportunity to come up with a better signature. The current implementation requires us to write odd constructions to get a handle of the push_frame inside of the incoming_handler.

May I suggest:

type connection

val send : connection -> Frame.t -> unit
val recv : connection -> Frame.t
val close : connection -> unit

val upgrade_connection :
  Http.Request.t ->
  (connection -> unit) ->
  Cohttp_eio.Server.response_action
(** [upgrade_connection req callback] takes [req], a connection request, and
    [callback], a function that will be called when the websocket upgrade is
    successful is established and returns [response_action] which is used to
    produce a {!Cohttp_eio.Server.t}. *)

| None -> raise End_of_file
| Some hdr -> read_frame ic oc mode hdr

module Connected_client = struct
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is unused.

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

Successfully merging this pull request may close these issues.

7 participants