https://github.com/mrinalxdev/websocket-rs
Websocket initials in Rust language
https://github.com/mrinalxdev/websocket-rs
Last synced: 11 months ago
JSON representation
Websocket initials in Rust language
- Host: GitHub
- URL: https://github.com/mrinalxdev/websocket-rs
- Owner: mrinalxdev
- Created: 2024-05-01T21:56:16.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-01T22:32:49.000Z (almost 2 years ago)
- Last Synced: 2025-01-20T17:21:53.567Z (about 1 year ago)
- Language: Rust
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# websocket-rs
Websocket initials in Rust language
### main.rs Documentation for learning
There is the websocket service stored in the url variable.
- To connect to the service asynchronusly we are using (connect_async) method.
> Connecting to the server .
- There will be lot of errors regarding the async functions used,
- First mark the main function under tokio `#[tokio::main]` by doing like this above the main function.
- `connect_async` is method under tokio-tungstenite, so import from that.
> Sending and recieving messages
- To send and recieve messages from the service agent, we need to split up the websocket stream into a write and read stream.
- `.split()` method is used for spliting the stream into multiple stream. Let's take two varaibles known (write)[For writing] and (read)[for reading]
- For writing messages to the stream
- (write) ``write.send(msg);`` is used, this is method used from ``futures_util::sink::SinkExt`` crate in rust.
- ``let msg = Message::Text()`` is used for declaring the message
- At last its an async function so do add ``write.send(msg).await`` at the end.
- For reading messages to the stream
- You can first check if there is any message by putting it in a if stattement, --> ``read.next()`` to pull out the messages
- If there is any than print the messages.
# Your Websocket is ready !! 🥳🥳🥳