This repository provides a template for setting up a Socket.IO server with adapter options for distributed deployment. It is designed to help you quickly deploy a scalable real-time communication system.
- Scalability: Easily scale your Socket.IO server across multiple nodes.
- Maintainability: Ensure high code quality and consistency.
- Security: Implement robust authentication and enforce data validation and type safety.
- Redis Adapter: Use Redis for pub/sub messaging between nodes.
- Docker Support: Containerize your application for consistent environments.
- Clone the repository:
git clone https://github.com/nivandres/socket-template.git
cd socket-template- Install dependencies:
bun install- Start the server:
bun run devThe server can be configured using environment variables. Here's an example configuration:
PORT=3000
CORS_ORIGIN=*
AUTH_SECRET=
INSTRUMENT_USERNAME=
INSTRUMENT_PASSWORD=
ADAPTER=redis
ADAPTER_URL=redis://localhost:6379PORT: The port number to listen on.CORS_ORIGIN: The origins to allow for cross-origin requests. Use a comma-separated list for multiple origins.AUTH_SECRET: The secret key for authentication (jsonwebtoken).INSTRUMENT_USERNAME: The username for the SocketIO and Redis instrumentation.INSTRUMENT_PASSWORD: The password for the SocketIO and Redis instrumentation.ADAPTER: The adapter to use for distributed pub/sub messaging.ADAPTER_URL: The URL for accessing the adapter.
To deploy the server locally, you can use Docker Compose:
PORT=3000 docker-compose up -dThis will start the server and Redis containers, and expose the server on port 3000.
You can access the server at http://localhost:3000.
Test the server here introducing your localhost url.
You can deploy the server to production using services such as Fly.io to scale easily.
- Fly launch your app:
fly launch- Configure the app:
fly secrets set AUTH_SECRET=your_secret_key
fly secrets set INSTRUMENT_USERNAME=your_instrument_username
fly secrets set INSTRUMENT_PASSWORD=your_instrument_password
fly secrets set ADAPTER_URL=redis://redis:6379- Deploy the app:
fly deployHere's an example of how to use the server:
// Path: /src/socket.ts
export function handleSocket(socket: Socket) {
socket.on("message", message => {
socket.broadcast.emit("message", message);
});
}Here's an example of how to connect to the server using Socket.IO:
const socket = io("http://localhost:3000");
socket.on("message", message => {
addMessage(message);
});Here's an example of how to connect to the server using an external Node.js process:
import { Emitter } from "@socket.io/redis-emitter";
import { createClient } from "redis";
const client = createClient();
const io = new Emitter(client);
io.emit("message", "Hello, world!");