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

Would be helpful if there was some usage examples #8

Closed
chmorgan opened this issue Mar 26, 2024 · 8 comments
Closed

Would be helpful if there was some usage examples #8

chmorgan opened this issue Mar 26, 2024 · 8 comments

Comments

@chmorgan
Copy link

I’m trying to make use of web sockets in a proof of concept dioxus app.

Tungstenite doesn’t appear to work out of the box with wasm.

The web sys web socket example in the handbook looked a bit crazy, loads of boxing and dyn usage but maybe that’s from an older version or maybe that’s just a side effect of interop with the browser. In any case I went looking for an alternative.

It looked like this crate might provide the Tokyo tungstenite interface, except using wasm, but this isn’t clear so I could be totally misunderstanding its purpose.

@TannerRogalsky
Copy link
Owner

I'm open to this but I'm not sure what examples to include as this crate is very simply a wrapper for browser websockets and tokio-tungenstenite websockets. What examples would you expect that aren't already present in the the projects that this wraps?

@chmorgan
Copy link
Author

Hi @TannerRogalsky.

I had a few questions such as "why would I use this library?" and also if its wrapping both interfaces is it a 3rd new type of interface or is it intended to have the same interface as tokio-tungstenite whether one is using tokio-tungstenite or the browser's interface via wasm?

It sounds like an interesting crate and I'm trying to do web sockets in wasm and was hoping this could be a crate I could use for that.

@TannerRogalsky
Copy link
Owner

why would I use this library?

I would consider "A wrapper around websys and tokio-tungstenite that makes it easy to use websockets cross-platform." to be a pretty concise answer to that question. Do you think I'm missing something?

if its wrapping both interfaces is it a 3rd new type of interface or is it intended to have the same interface as tokio-tungstenite whether one is using tokio-tungstenite or the browser's interface via wasm?

I feel like you're trying to ask something here that I'm not getting so I'll take this at face value.

  1. It's a third type, of course. This doesn't just export the underlying types directly. There would be little worth in such a thing.
  2. It closely resembles tokio-tungenstenite in it's API although limited to the client API. This is why the crate is named like it is.

I don't feel like these things are typically things that I see in a crate's readme but I'm open to be being convinced otherwise.

@chmorgan
Copy link
Author

No I think your description is accurate. What would have helped me would have been a sentence to clarify the api, maybe something like:

“The api provided by this crate is a hybrid of tungsrenite and web_sys, although it differs from their apis like zzz”.

I’m new to rust and it’s difficult still to understand the api via code vs looking at an example. What I was hoping for was tungstenite api but with wasm support and I’m getting that that isn’t the api/behavior here.

Again, thank you for creating and sharing this crate. Apologizes for asking dumb questions.

@TannerRogalsky
Copy link
Owner

TannerRogalsky commented Mar 27, 2024

It wasn't my intention that this be a good beginner crate but you're not the first person to ask for better examples so maybe it's worthwhile.

I appreciate your insights as a budding Rustacean. I'll certainly consider your words when I go to update this crate again.

@42Pupusas
Copy link

  • 1 to request some examples on the readme.

Its probably just a skill issue but I cant get this working.

I've been using tokio-tungstenite to handle WebSockets and was hoping to port my crate to WASM. Using your crate I managed to at least get the compiler happy, but I can't seem to actually get any tests running. Not sure why but the sockets refuse to connect.

Simplest example is:

    #[cfg(target_arch = "wasm32")]
    use tokio_tungstenite_wasm::{connect, Message, WebSocketStream};

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    async fn test_relay_events() {
        let urlstring = "wss://relay.arrakis.lat".to_string();
        let websocket = connect(urlstring)
            .await
            .expect("Failed to connect");
        assert!(true);
    }

failures:

---- main_async::tests::test_relay_events output ----
error output:
panicked at tests/main_async.rs:26:14:
Failed to connect: Url(UnsupportedUrlScheme)

@TannerRogalsky
Copy link
Owner

I've added an example #9. @chmorgan could you review it and give me feedback?

@42Pupusas The creation of the Websocket is erroring. The possible cases for that are outlined in the MDN. I'm pretty sure your error is because you're trying to connect to a different TLD than the one your code is running under. The details of cross-origin requests are firmly outside of the scope of this project, however.

@TannerRogalsky
Copy link
Owner

I'm closing this due to a lack of feedback. If you have further comments feel free to reopen.

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

3 participants