socket-ts
v1.0.0
Published
Socket.io server with Redis adapter in TypeScript
Readme
Socket.IO Server with Redis Adapter
This project is a TypeScript version of a Socket.IO server using Redis Adapter with the official Redis library.
Features
- Built with TypeScript
- Socket.IO with Redis Adapter to support multiple instances
- Uses the official Redis library (node-redis) instead of IORedis
- Supports both Redis Single and Redis Cluster modes
- Can switch between single and cluster modes at runtime
- JWT authentication
- MVC architecture
- Docker and Docker Compose for deployment
Installation
Installing Dependencies
npm installEnvironment Configuration
Copy the .env.example file to .env and configure the environment variables:
cp .env.example .envRedis Configuration
The application supports two Redis modes:
- Single mode - Connect to a single Redis server:
REDIS_MODE=single
REDIS_URI=redis://localhost:6379
REDIS_PASSWORD=your_redis_password- Cluster mode - Connect to a Redis Cluster:
REDIS_MODE=cluster
REDIS_PASSWORD=your_redis_password
REDIS_CLUSTER_NODES=127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382Switching Between Redis Modes
You can switch between Redis single and cluster modes at runtime by calling the API:
# View current configuration
curl http://localhost:3000/redis/config
# Switch to single mode
curl -X POST http://localhost:3000/redis/config \
-H "Content-Type: application/json" \
-d '{"mode":"single","uri":"redis://localhost:6379","password":"your_password"}'
# Switch to cluster mode
curl -X POST http://localhost:3000/redis/config \
-H "Content-Type: application/json" \
-d '{"mode":"cluster","clusterNodes":["127.0.0.1:6380","127.0.0.1:6381","127.0.0.1:6382"],"password":"your_password"}'Development
Run in Development Mode
npm run devBuild Project
npm run buildRun Built Application
npm startDeployment with Docker
Build and Run with Docker Compose
docker-compose up -dView Logs
docker-compose logs -fAPI
GET /: Returns server statusGET /health: Check server healthGET /redis/config: Get current Redis configurationPOST /redis/config: Update Redis configuration
Socket.IO Events
Client to Server
subscribe: Subscribe to receive messages from a roomunsubscribe: Unsubscribe from receiving messagesreaction: Send a reaction to a roomget_online_users: Get the list of online users in a room
Server to Client
history: Returns message history when client subscribesonline_users: Returns list of online users in a roomuser_joined: Notification when a user joins a roomuser_left: Notification when a user leaves a roomon_reaction: Sends reactions to all clients in a room
Directory Structure
socket-ts/
├── src/
│ ├── config/ # Application configuration
│ ├── controllers/ # Application logic
│ ├── interfaces/ # Type definitions
│ ├── middleware/ # Middleware
│ ├── models/ # Models
│ ├── routes/ # Router
│ ├── services/ # Services
│ ├── utils/ # Utility functions
│ └── index.ts # Application entry point
├── docker/ # Docker configurations
├── Dockerfile # Docker build file
├── docker-compose.yml # Docker Compose configuration
├── .env.example # Example environment variables
├── package.json # Dependencies and scripts
└── tsconfig.json # TypeScript configurationTest Tools
Redis Emitter Test Tool
A command-line tool for testing Socket.io with Redis adapter. This tool allows you to:
- Send messages to specific rooms
- Simulate users joining/leaving rooms
- Send reactions to messages
- Test custom events
To use the tool:
cd test-tools
npm install
npm startSee the test-tools/README.md for more details.
