{"id":27226142,"url":"https://github.com/egdavid/rusty-socks","last_synced_at":"2026-02-24T04:32:08.023Z","repository":{"id":282556770,"uuid":"948961344","full_name":"egdavid/rusty-socks","owner":"egdavid","description":"🧦 Rusty Socks: Real-time Communication Made Simple. Rusty Socks is a lightning-fast WebSocket server crafted with Rust. Born from my passion for efficient networking, this project combines the safety of Rust with the power of WebSockets to create a strong foundation for any real-time applications.","archived":false,"fork":false,"pushed_at":"2025-09-29T11:10:10.000Z","size":330,"stargazers_count":2,"open_issues_count":8,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-03T14:50:26.034Z","etag":null,"topics":["async","communication","networking","real-time","rust","rust-lang","server","tokio","warp","websocket","websocket-server"],"latest_commit_sha":null,"homepage":"http://rustysocks.io/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/egdavid.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":"ROADMAP.md","authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-03-15T11:06:22.000Z","updated_at":"2025-09-23T14:30:00.000Z","dependencies_parsed_at":"2025-04-10T11:47:26.930Z","dependency_job_id":"97d24270-5af0-44a8-8bef-6327e80fca04","html_url":"https://github.com/egdavid/rusty-socks","commit_stats":null,"previous_names":["egdavid/rusty-socks"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/egdavid/rusty-socks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egdavid%2Frusty-socks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egdavid%2Frusty-socks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egdavid%2Frusty-socks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egdavid%2Frusty-socks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/egdavid","download_url":"https://codeload.github.com/egdavid/rusty-socks/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egdavid%2Frusty-socks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29771908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-24T04:01:02.180Z","status":"ssl_error","status_checked_at":"2026-02-24T03:59:49.901Z","response_time":75,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","communication","networking","real-time","rust","rust-lang","server","tokio","warp","websocket","websocket-server"],"created_at":"2025-04-10T11:47:19.705Z","updated_at":"2026-02-24T04:32:07.993Z","avatar_url":"https://github.com/egdavid.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rusty Socks\n\n[![CI](https://github.com/egdavid/rusty-socks/workflows/CI/badge.svg)](https://github.com/egdavid/rusty-socks/actions)\n[![Crates.io](https://img.shields.io/crates/v/rusty-socks.svg)](https://crates.io/crates/rusty-socks)\n[![Documentation](https://docs.rs/rusty-socks/badge.svg)](https://docs.rs/rusty-socks)\n[![License](https://img.shields.io/crates/l/rusty-socks.svg)](https://github.com/egdavid/rusty-socks/blob/main/LICENSE)\n[![Downloads](https://img.shields.io/crates/d/rusty-socks.svg)](https://crates.io/crates/rusty-socks)\n\nA high-performance, production-ready WebSocket server written in Rust, designed for real-time applications across gaming, finance, chat, and other domains requiring fast, secure communication.\n\n## Features\n\n### Core Capabilities\n- **High-Performance WebSocket Server** - Built with Tokio and Warp for maximum throughput\n- **Room-Based Communication** - Organize users into channels/rooms with fine-grained permissions\n- **JWT Authentication** - Secure token-based authentication with role-based access control (RBAC)\n- **Rate Limiting** - Prevent abuse with configurable per-user and global rate limits\n- **Message Validation** - XSS protection, spam detection, and content filtering\n- **Thread-Safe Architecture** - Concurrent message handling with race condition protection\n\n### Security Features\n- **Production-Ready Security** - Comprehensive vulnerability protection\n- **Role-Based Permissions** - Owner, Admin, Moderator, Member, and Guest roles\n- **Ban/Kick/Mute System** - Complete moderation toolkit\n- **Connection Limiting** - Prevent DoS attacks with IP-based connection limits\n- **Input Validation** - Protect against injection and malformed data\n\n### Scalability\n- **Configurable Thread Pool** - Optimize performance for your hardware\n- **Memory Protection** - Built-in safeguards against memory exhaustion\n- **Async Broadcasting** - Efficient message distribution to large user groups\n- **Resource Cleanup** - Automatic cleanup of idle connections and expired data\n\n## Architecture\n\nThe server is built on the following components:\n\n- **Authentication**: JWT token management with role-based access control\n- **Core**: Room management, session handling, connection processing, and thread pooling\n- **Handlers**: WebSocket and HTTP request processing with authentication\n- **Storage**: Simple in-memory message persistence with room isolation\n- **Configuration**: Dynamic server settings through environment variables\n\n## Prerequisites\n\n- Rust (1.63.0 or newer)\n- Cargo package manager\n\n## Installation\n\nClone the repository and build the project:\n\n```bash\ngit clone https://github.com/egdavid/rusty-socks.git\ncd rusty-socks\ncargo build --release\n```\n\n## Configuration\n\nRusty Socks can be configured using environment variables:\n\n| Variable | Description | Default |\n|----------|-------------|---------|\n| RUSTY_SOCKS_HOST | Server host address | 0.0.0.0 |\n| RUSTY_SOCKS_PORT | Server port | 3030 |\n| RUSTY_SOCKS_MAX_CONN | Maximum connections | 100 |\n| RUSTY_SOCKS_BUFFER | Message buffer size | 1024 |\n| RUSTY_SOCKS_TIMEOUT | Connection timeout in seconds | 60 |\n| RUSTY_SOCKS_PING | Ping interval in seconds | 30 |\n| RUSTY_SOCKS_THREAD_POOL_SIZE | Number of worker threads in the pool | 4 |\n| RUSTY_SOCKS_MAX_QUEUED_TASKS | Maximum number of tasks that can be queued | 1000 |\n| RUSTY_SOCKS_JWT_SECRET | Secret key for JWT token signing | \"your-secret-key\" |\n\n## Usage\n\n### Running the server\n\n```bash\ncargo run --bin rusty_socks\n```\n\nOr with custom configuration:\n\n```bash\nRUSTY_SOCKS_PORT=8080 RUSTY_SOCKS_THREAD_POOL_SIZE=8 cargo run --bin rusty_socks\n```\n\n### Connecting to the server\n\nWebSocket endpoint is available at:\n\n```\nws://[host]:[port]/ws\n```\n\nHealth check endpoint:\n\n```\nhttp://[host]:[port]/health\n```\n\nThread pool statistics endpoint:\n\n```\nhttp://[host]:[port]/stats\n```\n\n### Connection Handling and Rejection\n\nRusty Socks uses a thread pool to efficiently manage multiple concurrent WebSocket connections. When the server is under heavy load:\n\n1. New WebSocket connections are queued if all worker threads are busy\n2. If the connection queue reaches its maximum capacity (`RUSTY_SOCKS_MAX_QUEUED_TASKS`), **new connection attempts will be rejected**\n3. Rejected clients will experience a connection failure\n4. Existing connections remain unaffected and continue to function normally\n\n**Important for client implementations:**\n- Implement connection retry logic with exponential backoff\n- Add appropriate error handling for connection failures\n- Consider monitoring connection rejection rates in production environments\n\nThis connection rejection mechanism is a deliberate design choice to maintain server stability and responsiveness for existing connections during peak loads, rather than risking degraded performance for all users.\n\n### Authentication\n\nRusty Socks uses JWT tokens for authentication. To connect to the WebSocket server:\n\n1. **Obtain a JWT token** (implement your own authentication endpoint)\n2. **Include the token** in the WebSocket connection URL as a query parameter:\n   ```\n   ws://localhost:3030/ws?token=your_jwt_token_here\n   ```\n\n### Client example (JavaScript)\n\n```javascript\n// Assuming you have a JWT token from your auth system\nconst token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';\nconst socket = new WebSocket(`ws://localhost:3030/ws?token=${token}`);\n\nsocket.onopen = function() {\n  console.log('Connected to Rusty Socks server');\n  \n  // Join a room\n  const joinMessage = {\n    type: 'join_room',\n    room_id: 'general',\n    password: null // optional for password-protected rooms\n  };\n  socket.send(JSON.stringify(joinMessage));\n  \n  // Send a message to the room\n  const message = {\n    type: 'room_message',\n    room_id: 'general',\n    content: 'Hello from JS client',\n    timestamp: new Date().toISOString()\n  };\n  socket.send(JSON.stringify(message));\n};\n\nsocket.onmessage = function(event) {\n  const message = JSON.parse(event.data);\n  console.log('Received:', message);\n  \n  // Handle different message types\n  switch(message.type) {\n    case 'room_message':\n      console.log(`[${message.room_id}] ${message.sender}: ${message.content}`);\n      break;\n    case 'user_joined':\n      console.log(`${message.username} joined ${message.room_id}`);\n      break;\n    case 'error':\n      console.error('Server error:', message.message);\n      break;\n  }\n};\n\nsocket.onclose = function() {\n  console.log('Connection closed');\n};\n\nsocket.onerror = function(error) {\n  console.error('WebSocket error:', error);\n  // Implement exponential backoff retry here\n};\n```\n\n### Room Management\n\nUsers with appropriate permissions can manage rooms:\n\n```javascript\n// Create a new room (requires ManageRoom permission)\nconst createRoom = {\n  type: 'create_room',\n  name: 'My Private Room',\n  is_private: true,\n  max_members: 50\n};\nsocket.send(JSON.stringify(createRoom));\n\n// Set user role (requires ManageRoles permission)\nconst setRole = {\n  type: 'set_user_role',\n  room_id: 'general',\n  user_id: 'target_user_id',\n  role: 'Moderator'\n};\nsocket.send(JSON.stringify(setRole));\n\n// Ban user (requires BanUsers permission)\nconst banUser = {\n  type: 'ban_user',\n  room_id: 'general',\n  user_id: 'target_user_id',\n  duration_hours: 24 // optional, null for permanent\n};\nsocket.send(JSON.stringify(banUser));\n```\n\n## Performance\n\nRusty Socks is designed for high performance with a configurable thread pool that:\n\n- Distributes connection handling across multiple worker threads\n- Controls maximum task queue size to prevent server overload\n- Provides monitoring through the `/stats` endpoint\n- Efficiently utilizes multi-core processors\n\nTo optimize performance, adjust the thread pool settings based on your hardware:\n\n```bash\n# For a machine with 8 cores\nRUSTY_SOCKS_THREAD_POOL_SIZE=8 RUSTY_SOCKS_MAX_QUEUED_TASKS=2000 cargo run --bin rusty_socks\n```\n\n## Testing\n\n### Automated Tests\n\nRun the integration tests:\n\n```bash\ncargo test\n```\n\nRun specific tests:\n\n```bash\ncargo test --test websocket_test\n```\n\n### Manual WebSocket testing using wscat\n\nYou can use `wscat` to manually test the WebSocket server functionality. This is particularly useful for debugging and verifying real-time message exchange.\n\n#### Installation\n\nInstall wscat using npm:\n\n```bash\nnpm install -g wscat\n```\n\n#### Basic Connection Testing\n\nConnect to the WebSocket server:\n\n```bash\nwscat -c ws://localhost:3030/ws\n```\n\n#### Testing Message Exchange\n\n1. Start the server:\n   ```bash\n   cargo run --bin rusty_socks\n   ```\n\n2. Connect with a client:\n   ```bash\n   wscat -c ws://localhost:3030/ws\n   ```\n\n3. After connecting, you should receive a welcome message with your client ID.\n\n4. Send a test message (should be properly formatted JSON):\n   ```json\n   {\"id\":\"00000000-0000-0000-0000-000000000000\",\"sender\":\"test_user\",\"content\":\"Hello from wscat!\",\"timestamp\":\"2025-03-15T12:00:00Z\"}\n   ```\n\n5. Any response from the server will be displayed in the terminal.\n\n#### Testing Multiple Clients\n\nFor testing broadcast functionality, open multiple terminal sessions with wscat connections and observe how messages are distributed among clients.\n\n#### Connection Options\n\nConnect with verbose output for debugging:\n```bash\nwscat -c ws://localhost:3030/ws --verbose\n```\n\nConnect to a custom port:\n```bash\nwscat -c ws://localhost:8080/ws\n```\n\n## Load Testing\n\nThe server's thread pool allows it to handle multiple concurrent connections efficiently. To test the server under load:\n\n1. Install a load testing tool like `artillery` or `vegeta`\n2. Run the load test against the WebSocket endpoint\n3. Monitor the server's thread pool stats during the test:\n   ```bash\n   curl http://localhost:3030/stats\n   ```\n\n4. To simulate connection rejection scenarios:\n   ```bash\n   # Run with a small thread pool and queue size\n   RUSTY_SOCKS_THREAD_POOL_SIZE=2 RUSTY_SOCKS_MAX_QUEUED_TASKS=10 cargo run --bin rusty_socks\n   \n   # Then send many simultaneous connection requests\n   # Observe which ones are accepted and which are rejected\n   ```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request 🙃\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegdavid%2Frusty-socks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fegdavid%2Frusty-socks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegdavid%2Frusty-socks/lists"}