Skip to content

First draft of RFC for websockets API. #113

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

Closed
wants to merge 2 commits into from

Conversation

richard-uk1
Copy link
Contributor

@richard-uk1 richard-uk1 commented Feb 29, 2020

This is a first draft of a websocket API. I don't think it's ready to merge yet, but I wanted to start the conversation.

rendered

@richard-uk1
Copy link
Contributor Author

Can someone explain to me how to link the rendered document without it being tied to a specific blob?

@richard-uk1 richard-uk1 mentioned this pull request Mar 8, 2020
@richard-uk1
Copy link
Contributor Author

ping

@lemmih
Copy link

lemmih commented May 26, 2021

I would like to assist. The draft of the API looks complete enough for an experimental implementation. What would be the best next steps for this?

@ranile
Copy link
Collaborator

ranile commented May 26, 2021

I just saw this. I would suggest using an API based on channels/streams as that's consistent with other websocket crates and, imo, is more "rust-y". See this implementation of such an API in reqwasm as an example.

@lemmih
Copy link

lemmih commented May 26, 2021

The futures API from the proposal is consistent with the API from reqwasm, right?

@ranile
Copy link
Collaborator

ranile commented May 26, 2021

Seems that way. I somehow missed the figured part. This API looks good.

Copy link
Collaborator

@ranile ranile left a comment

Choose a reason for hiding this comment

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

I left a few comments. Generally, it looks good.

Comment on lines +184 to +188
/// Lose access to the websocket but keep the callbacks in case any events are recieved.
///
/// It's best not to use this function in production, as the callbacks and possibly the
/// websocket itself will leak. TODO should this method exist at all?
pub fn forget(mut self) { .. }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Imo, this shouldn't exist. We should store the callbacks and drop them when the WebSocket instance is dropped.

Comment on lines +151 to +154
on_open: impl FnOnce() + 'static,
mut on_error: impl FnMut() + 'static,
on_close: impl FnOnce(CloseEvent) + 'static,
mut on_message: impl FnMut(Message) + 'static,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be using a builder? I'm not sure if forcing the user to define empty callbacks is a good idea.

pub fn forget(mut self) { .. }

/// Start the closing handshake.
pub fn close(&self) { .. }
Copy link
Collaborator

Choose a reason for hiding this comment

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

close should take ownership so the instance can't be used for being closed.

Suggested change
pub fn close(&self) { .. }
pub fn close(self) { .. }

/// The code must be *1000* or between *3000* and *4999* inclusive, and the reason string, if
/// present, must be less than or equal to 123 bytes in length (when utf8 encoded). If either
/// of these conditions are violated, the function will error without closing the connection.
pub fn close_with_reason(&self, code: u16, reason: Option<&str>) -> Result<(), CloseError> { .. }
Copy link
Collaborator

Choose a reason for hiding this comment

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

close_with_reason, like close, should take ownership so the instance can't be used for being closed.

Suggested change
pub fn close_with_reason(&self, code: u16, reason: Option<&str>) -> Result<(), CloseError> { .. }
pub fn close_with_reason(self, code: u16, reason: Option<&str>) -> Result<(), CloseError> { .. }

pub struct WebSocket { .. }

impl Stream for WebSocket {
type Item = Result<Message, ConnectionError>;
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 for outgoing messages, right?

Comment on lines +244 to +245
The main alternative here is that we could use a bespoke model for the futures websocket, rather than
using standard traits (Stream + Sink). I'm not sure exactly which would be preferable.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer to keep the API using standard traits.

The main alternative here is that we could use a bespoke model for the futures websocket, rather than
using standard traits (Stream + Sink). I'm not sure exactly which would be preferable.

## Unresolved Questions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrong heading?


```rust

/// An incoming websocket message.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems this is also used for outgoing messages (see Stream impl comment) so this is a misleading doc comment?

@ranile
Copy link
Collaborator

ranile commented Jun 25, 2021

Another thing: @derekdreery, can you create an issue for this RFC with the changes. See #136 for details

@ranile ranile mentioned this pull request Aug 20, 2021
3 tasks
@ranile ranile mentioned this pull request Feb 15, 2022
@ranile ranile closed this in #191 Feb 16, 2022
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.

3 participants