A lightweight WebSocket echo server built in Rust using Tokio and tokio-tungstenite. This server listens for WebSocket connections, processes messages based on the endpoint, and provides multiple functionalities.
- Concurrent WebSocket Clients: Supports multiple WebSocket clients simultaneously.
- Echo Functionality: Automatically echoes back text and binary messages.
- Mathematical Operations: Provides an endpoint for solving basic math operations.
- Global Chat: Facilitates a simple chat feature for connected clients.
- Asynchronous and Efficient: Powered by Tokio for high performance.
- Docker Support: Easily deployable using Docker for any environment.
- Modular and Extensible: Clean Rust codebase for adding new features.
- Rust (version
1.70+
) - Cargo (Rust's package manager)
- Docker (for containerized deployment)
-
Clone the repository:
git clone https://github.com/LukaJioshvili20/rust-websocket-server.git cd rust-websocket-server
-
Build the project:
cargo build --release
-
Start the WebSocket server:
cargo run
-
The server will start on
ws://0.0.0.0:8765
. -
Test the server using a WebSocket client. For example, with wscat:
npm install -g wscat
-
Connect to different endpoints:
-
Establish a connection:
wscat -c ws://0.0.0.0:8765/echo
-
Send a message:
> Hello, WebSocket!
The server will echo it back:
< Echo: Hello, WebSocket!
-
Perform mathematical operations:
wscat -c ws://0.0.0.0:8765/math
-
Example interactions:
> 8 - 2 < Result: 6 > 10 + 2 < Result: 12 > 10 * 2 < Result: 20 > 3! < Result: 6
-
Engage in a global chat:
wscat -c ws://0.0.0.0:8765/global-chat
-
Example chat:
> Hello Somebody < Greeting from somebody > My name is Jeff < My name is John
-
You can also run the server in a Docker container:
-
Build the Docker image:
docker build -t websocket-echo-server .
-
Run the container:
docker run -p 8765:8765 websocket-echo-server
-
The server will be available at
ws://<your-docker-host>:8765
.
This project uses the following Rust crates:
tokio
: Asynchronous runtime.tokio-tungstenite
: WebSocket implementation for Tokio.futures-util
: Utilities for working with asynchronous streams.tracing
: For structured logging.http
: Utilities for working with HTTP requests and responses.uuid
: For generating and handling UUIDs, especially useful for unique client or session identifiers.
main.rs
: Contains the main WebSocket server logic, including:- Setting up the TCP listener.
- Accepting WebSocket connections.
- Handling
/echo
,/math
, and/global-chat
endpoints.
Enhance the server by:
- Secure Communication:
- Add WSS support with
tokio-rustls
.
- Add WSS support with
When running the server, the output will look like this:
INFO WebSocket server started successfully on ws://0.0.0.0:8765
INFO /echo is ready to be listened to...
INFO Incoming connection on path: /echo
INFO Echo handler invoked
INFO Received: Text("Hello, WebSocket!")
INFO Echo connection closed
Contributions are welcome! Feel free to submit issues or pull requests to improve the project.
This project is licensed under the MIT License.