-
Notifications
You must be signed in to change notification settings - Fork 957
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
feat: add websocket transport for browser environment based on web-sys
#3679
Conversation
I only see two dependents. One is our meta crate and the other one hasn't see a release in two years and depends on a really old version. I suspect it is unused. To make things easier in case someone does depend on it, we should deprecate it first and only remove it later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I've left some comments and questions :)
transports/websys/src/websocket.rs
Outdated
} | ||
|
||
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> { | ||
Poll::Pending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to return Poll::Ready
eventually here, otherwise we will never be able to close these streams. I think we should:
- Check if we are closed, if yes return
Poll::Ready
- If not and we are also not
closing
, callWebsocket::close
and set aclosing
flag to true - If we are
closing
, returnPoll::Pending
and set a waker - Once we are called in
on_close
, call the waker
transports/websys/src/websocket.rs
Outdated
opened: false, | ||
closed: false, | ||
error: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this perhaps be an enum instead of multiple flags? As far as I can tell, these are mutually exclusive.
Thanks Thomas these are very helpful comments, I'll make the changes in the next few days. |
web-sys
web-sys
web-sys
Let me know when this is ready for another review! |
Thanks Thomas, I have renamed the package as you suggested to I am a bit busy at the moment but I'll try to get more done this weekend, I'll ping you once the code is ready for another review. |
Thanks for the suggestion, done now: https://crates.io/crates/libp2p-websys-websocket |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks promising. Thanks for the work!
Thanks @thomaseizinger. If I recall correctly, members of a GitHub team can not add other people as owners on crates.io. For the sake of consistency, given that I am an owner of all |
Done. |
@thomaseizinger thank you for helping with this, I have tried the new version but it looks that if you connect from the browser and then kill the server the client doesn't get a disconnection event. With the old version if I connect send hello and then kill the server I get this: but with the new version nothing happen, I can see that if we always set the write waker like: fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let mut shared = self.shared.lock();
shared.write_waker = Some(cx.waker().clone()); then it works but I am not sure if that is the right thing to do. |
@vincev Thank you for testing! I did run your chat app as well after my refactorings but I didn't test the disconnection. Let me look into it. |
Well, technically it is overkill but it also doesn't hurt per se. Generally, wakers should only be requested when necessary, otherwise, it is hard to understand when and why we are meant to wake a task. Looking at the code now, something can't be quite right with what we implemented. At the moment, we don't have a branch that can return |
Wow, the documentation says that the browser will simply close the connection if we overload it with too much data haha: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send So apparently, the solution is to just choose some buffer size and check periodically if I am currently trying to clear out our backlog for the next breaking release so I don't have much capacity to implement this unfortunately. As it stands, the current solution isn't great because we don't have good backpressure. A client will simply send as much data as it can and if the browser can't keep up, it will close the connection :( |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry for not engaging here any earlier. Overall this looks good to me. Though, unless I am missing something, the above documented issue around I did a quick scan over the js-libp2p code-base but couldn't find the corresponding handling there. Maybe @achingbrain or @MarcoPolo do you know where js-libp2p is handling WebSocket's |
Yes I see it the same way. |
Yes this looks like a problem, probably for a simple chat example app like the one I wrote a simple websocket will do, hopefully you can reuse some of this code in the future when you expand |
@vincev Did you delete the branch? I'd actually like to have this merged eventually, I just haven't had the time yet to finish it :) |
Oh gosh I interpreted yours and Max comment as a big blocker from websys for the implementation. Let me see if I can restore it. |
It is a blocker for merging as is and the fix isn't pretty because it involves an arbitrary buffer limit and a timer but it is doable so it is not a fundamental blocker :) |
Okay got it, I have raised a ticket with github I'll let you know as soon as is back. |
@thomaseizinger repo is back. |
@vincev thank you. Sorry for not being clear. Your work here will still be useful! |
@mxinden no problem at all, happy to help. |
I am going to close this PR as I am not doing any work on this. |
|
Thank you @thomaseizinger. |
Description
Resolves #3611.
Notes & open questions
To enable this feature in the example:
I tested the changes with the wasm-p2p-chat example but running tests automatically is not easy as it requires a headless browser.
Note I didn't remove
wasm-ext
as it looks from crates.io that there are people using it, maybe we can deprecate it.Change checklist