@sametnurel/socket-io-game-server
v0.0.8
Published
A lightweight, real-time multiplayer game server built with Socket.IO.
Downloads
15
Maintainers
Readme
🎮 Socket.IO Game Server
A modular, TypeScript-based Socket.IO game server framework, designed for managing real-time multiplayer games with a clean separation of concerns.
📦 Installation
npm install @sametnurel/socketio-game-server🚀 Features
- 🔌 WebSocket connection management via Socket.IO
- 🧩 Message handling using the Command pattern
- 🎮 Game instance lifecycle management
- 🗂 Singleton-based global managers
- 🔧 Easily extensible for various game types
🧱 Architecture Overview
+----------------+ +-------------------+
| Socket.IO |<------>| ConnectionManager|
+----------------+ +-------------------+
|
v
+------------------+
| IOManager |
+------------------+
|
v
+------------------+
| GameManager |
+------------------+
|
v
+----------------------+
| Game / Player Logic |
+----------------------+📡 ConnectionManager
Manages active connections and maps sockets to user IDs.
ConnectionManager.init(new MyConnectionManager());
export class MyConnectionManager extends ConnectionManager {
initCommands() {
.
.
.
this.initCommand(SoloTestMessages.InitPlayer, new InitPlayerCommand());
this.initCommand(SoloTestMessages.Disconnect, new DisconnectCommand());
this.initCommand(SoloTestMessages.LobbyChat, new LobbyChatCommand());
this.initCommand(SoloTestMessages.ChallengeUser, new ChallengeCommand());
.
.
.
}
}- Auto-registers event listeners per socket
- Maps userId ↔ connectionId
- Dispatches messages to
BaseCommandhandlers
🎮 GameManager
Manages the lifecycle of game instances.
GameManager.init(new MyGameManager());
const game = GameManager.getInstance().create();- Unique game ID generation
- Tracks all active games
- Supports custom game cleanup via
clearGame()
🔄 IOManager
Initializes the HTTP + WebSocket server.
IOManager.init();- Broadcast messages to rooms:
IOManager.instance.broadcastToGame(gameId, Messages.UPDATE, payload);🧩 BaseCommand
Extendable class to encapsulate message handling logic.
class JoinGameCommand extends BaseCommand<MyGameManager, MyConnectionManager> {
handle(socket, conn, msg) {
const game = this.gameManager.getGame(msg.gameId);
// game logic...
}
}- Receives
socket,Connection, and message - Access to all managers
📄 Example Project Structure
src/
├── base/
│ └── BaseCommand.ts
├── components/
│ ├── Connection.ts
│ └── Game.ts
├── managers/
│ ├── ConnectionManager.ts
│ ├── GameManager.ts
│ └── IOManager.ts
└── utility/
└── IdGenerator.ts🛠 Extending for Your Game
- Subclass
GameManager,ConnectionManager - Implement your
Game,Player,Commands - Register commands via
initCommands() - Run server using
IOManager.init()
📜 License
Samet Nurel
