Registry / messaging / socket-controllers

socket-controllers

JSON →
library0.3.1jsnpmunverified

A TypeScript library that provides class-based controllers for handling WebSocket events using decorators. Current stable version is 0.3.1. Developed by the TypeStack team, it allows organizing WebSocket handling in a structured, object-oriented manner similar to Express or routing-controllers. It relies on reflect-metadata and optionally a DI container like TypeDI. It supports socket.io and integrates well with TypeScript projects. Differentiators include declarative decorators (@OnConnect, @OnMessage) and automatic dependency injection.

npm install socket-controllers
INSTALL
IMPORT
SIG · SOCKET-CONTROLLERS
S
socket-controllers
messagingjavascriptv0.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SocketControllers
import { SocketControllers } from 'socket-controllers'
const SocketControllers = require('socket-controllers').SocketControllers
ESM default. CommonJS destructuring is also possible but ESM is encouraged.
OnConnect
import { OnConnect } from 'socket-controllers'
import OnConnect from 'socket-controllers'
Named export, not default.
OnMessage
import { OnMessage } from 'socket-controllers'
import { onMessage } from 'socket-controllers'
Decorator names are PascalCase.
SocketController
import { SocketController } from 'socket-controllers'
import { socketController } from 'socket-controllers'
Decorator names are PascalCase.
ConnectedSocket
import { ConnectedSocket } from 'socket-controllers'
import { ConnectedSocket } from 'socket-controllers'
Parameter decorator, not a component.
MessageBody
import { MessageBody } from 'socket-controllers'
Parameter decorator.

Sets up a basic socket server using socket-controllers with a single controller handling connect, disconnect, and a 'chat' message event.

import 'reflect-metadata'; import { SocketControllers } from 'socket-controllers'; import { SocketController, OnConnect, OnDisconnect, OnMessage, ConnectedSocket, MessageBody } from 'socket-controllers'; @SocketController() export class MyController { @OnConnect() onConnect(@ConnectedSocket() socket: any) { console.log('Client connected'); } @OnDisconnect() onDisconnect(@ConnectedSocket() socket: any) { console.log('Client disconnected'); } @OnMessage('chat') onChat(@ConnectedSocket() socket: any, @MessageBody() message: any) { console.log('Received:', message); socket.emit('chat', { reply: 'Message received' }); } } new SocketControllers({ port: 3000, controllers: [MyController], });
Debug
Known issues
gotchaRequires reflect-metadata import globally
fix
Add 'import "reflect-metadata"' at the entry point of your application.
affects: >=0.0.1
gotchaDecorator arguments must use @ConnectedSocket() and @MessageBody() with parentheses
fix
Always include parentheses: @ConnectedSocket() and @MessageBody(), even if no parameters.
affects: >=0.0.1
gotchaControllers must be decorated with @SocketController()
fix
Apply @SocketController() decorator to each controller class.
affects: >=0.0.1
gotchaDI container required for non-default constructor parameters
fix
Install typedi and pass container in SocketControllers options.
affects: >=0.0.1
gotchasocket-controllers does not work with plain JavaScript; TypeScript required
fix
Write code in TypeScript and ensure emitDecoratorMetadata and experimentalDecorators are enabled in tsconfig.json.
affects: >=0.0.1
Errors
Common errors & fixes
Reflect.getMetadata is not a function
reflect-metadata shim not imported
fix
Add 'import "reflect-metadata"' at the top of your main file.
Cannot use decorators in a TypeScript project without enabling experimentalDecorators
Decorator support not enabled in tsconfig.json
fix
Set 'experimentalDecorators': true and 'emitDecoratorMetadata': true in tsconfig.json.
Class constructor MyController cannot be invoked without 'new'
Controller not registered or DI container not configured
fix
Ensure controller is listed in controllers array and, if using DI, pass container: Container in SocketControllers options.
The 'connectedSocket' decorator must be used with a class and a property
Decorator used incorrectly
fix
Use @ConnectedSocket() as a parameter decorator inside a method, not on a property.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies
reflect-metadatarequiredRequired for decorator metadata reflection used by socket-controllers
typedioptionalOptional DI container for dependency injection in controllers
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
socket-controllers — npm install socket-controllers · libregistry