Skip to content

Repository files navigation

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 install

Environment Configuration

Copy the .env.example file to .env and configure the environment variables:

cp .env.example .env

Redis Configuration

The application supports two Redis modes:

  1. Single mode - Connect to a single Redis server:
REDIS_MODE=single
REDIS_URI=redis://localhost:6379
REDIS_PASSWORD=your_redis_password
  1. 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:6382

Switching 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 dev

Build Project

npm run build

Run Built Application

npm start

Deployment with Docker

Build and Run with Docker Compose

docker-compose up -d

View Logs

docker-compose logs -f

API

  • GET /: Returns server status
  • GET /health: Check server health
  • GET /redis/config: Get current Redis configuration
  • POST /redis/config: Update Redis configuration

Socket.IO Events

Client to Server

  • subscribe: Subscribe to receive messages from a room
  • unsubscribe: Unsubscribe from receiving messages
  • reaction: Send a reaction to a room
  • get_online_users: Get the list of online users in a room

Server to Client

  • history: Returns message history when client subscribes
  • online_users: Returns list of online users in a room
  • user_joined: Notification when a user joins a room
  • user_left: Notification when a user leaves a room
  • on_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 configuration

Test 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 start

See the test-tools/README.md for more details.

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages