@@ -25,6 +25,12 @@ Use class-based controllers to handle websocket events. Helps to organize your c
2525 ``` typescript
2626 import ' reflect-metadata' ;
2727 ```
28+
29+ 3 . Install a DI container, for example ` typedi ` ;
30+
31+ ```
32+ npm install typedi
33+ ```
2834
2935## Example of usage
3036
@@ -39,8 +45,10 @@ Use class-based controllers to handle websocket events. Helps to organize your c
3945 MessageBody ,
4046 OnMessage ,
4147 } from ' socket-controllers' ;
48+ import {Service } from ' typedi' ; // Only if you are using typedi
4249
4350 @SocketController ()
51+ @Service () // Only if you are using typedi
4452 export class MessageController {
4553 @OnConnect ()
4654 connection(@ConnectedSocket () socket : any ) {
@@ -67,10 +75,13 @@ Use class-based controllers to handle websocket events. Helps to organize your c
6775 ``` typescript
6876 import ' es6-shim' ; // this shim is optional if you are using old version of node
6977 import ' reflect-metadata' ; // this shim is required
70- import { createSocketServer } from ' socket-controllers' ;
71- import { MessageController } from ' ./MessageController' ;
78+ import { SocketControllers } from ' socket-controllers' ;
79+ import { MessageController } from ' ./MessageController' ;
80+ import {Container } from ' typedi' ; // Only if you are using typedi
7281
73- createSocketServer (3001 , {
82+ new SocketControllers ({
83+ port: 3001 ,
84+ container: Container ,
7485 controllers: [MessageController ],
7586 });
7687 ```
@@ -135,7 +146,7 @@ export class MessageController {
135146
136147If you specify a class type to parameter that is decorated with ` @MessageBody() ` ,
137148socket-controllers will use [ class-transformer] [ 1 ] to create instance of the given class type with the data received in the message.
138- To disable this behaviour you need to specify a ` { useConstructorUtils: false } ` in SocketControllerOptions when creating a server.
149+ To disable this behaviour you need to specify a ` { transformOption: { transform: false ] } ` in SocketControllerOptions when creating a server.
139150
140151#### ` @SocketQueryParam() ` decorator
141152
@@ -266,16 +277,18 @@ If promise returned by controller action, message will be emitted only after pro
266277#### Using exist server instead of creating a new one
267278
268279If you need to create and configure socket.io server manually,
269- you can use ` useSocketServer ` instead of ` createSocketServer ` function .
280+ you can pass it to the ` SocketControllers ` constructor .
270281Here is example of creating socket.io server and configuring it with express:
271282
272283``` typescript
273284import ' reflect-metadata' ; // this shim is required
274- import { useSocketServer } from ' socket-controllers' ;
285+ import { SocketControllers } from ' socket-controllers' ;
286+ import { Server } from ' socket.io' ;
287+ import { Container } from ' typedi' ; // Only if you are using typedi
275288
276289const app = require (' express' )();
277290const server = require (' http' ).Server (app );
278- const io = require ( ' socket.io ' ) (server );
291+ const io = new Server (server );
279292
280293server .listen (3001 );
281294
@@ -287,7 +300,7 @@ io.use((socket: any, next: Function) => {
287300 console .log (' Custom middleware' );
288301 next ();
289302});
290- useSocketServer ( io );
303+ new SocketControllers ({ io , container: Container } );
291304```
292305
293306#### Load all controllers from the given directory
@@ -297,9 +310,12 @@ You can load all controllers in once from specific directories, by specifying ar
297310
298311``` typescript
299312import ' reflect-metadata' ; // this shim is required
300- import { createSocketServer } from ' socket-controllers' ;
313+ import { SocketControllers } from ' socket-controllers' ;
314+ import { Container } from ' typedi' ; // Only if you are using typedi
301315
302- createSocketServer (3000 , {
316+ new SocketControllers ({
317+ port: 3000 ,
318+ container: Container ,
303319 controllers: [__dirname + ' /controllers/*.js' ],
304320}); // registers all given controllers
305321```
@@ -362,10 +378,13 @@ Controllers and middlewares should be loaded:
362378
363379``` typescript
364380import ' reflect-metadata' ;
365- import { createSocketServer } from ' socket-controllers' ;
381+ import { SocketControllers } from ' socket-controllers' ;
366382import { MessageController } from ' ./MessageController' ;
367383import { MyMiddleware } from ' ./MyMiddleware' ; // here we import it
368- let io = createSocketServer (3000 , {
384+ import { Container } from ' typedi' ; // Only if you are using typedi
385+ const server = new SocketControllers ({
386+ port: 3000 ,
387+ container: Container ,
369388 controllers: [MessageController ],
370389 middlewares: [MyMiddleware ],
371390});
@@ -375,10 +394,13 @@ Also you can load them from directories. Also you can use glob patterns:
375394
376395``` typescript
377396import ' reflect-metadata' ;
378- import { createSocketServer } from ' socket-controllers' ;
379- let io = createSocketServer (3000 , {
380- controllers: [__dirname + ' /controllers/**/*.js' ],
381- middlewares: [__dirname + ' /middlewares/**/*.js' ],
397+ import { SocketControllers } from ' socket-controllers' ;
398+ import { Container } from ' typedi' ; // Only if you are using typedi
399+ const server = new SocketControllers ({
400+ port: 3000 ,
401+ container: Container ,
402+ controllers: [__dirname + ' /controllers/**/*.js' ],
403+ middlewares: [__dirname + ' /middlewares/**/*.js' ],
382404});
383405```
384406
@@ -390,15 +412,13 @@ Here is example how to integrate socket-controllers with [typedi](https://github
390412
391413``` typescript
392414import ' reflect-metadata' ;
393- import { createSocketServer , useContainer } from ' socket-controllers' ;
415+ import { SocketControllers } from ' socket-controllers' ;
394416import { Container } from ' typedi' ;
395417
396- // its important to set container before any operation you do with socket-controllers,
397- // including importing controllers
398- useContainer (Container );
399-
400418// create and run socket server
401- let io = createSocketServer (3000 , {
419+ const server = new SocketControllers ({
420+ port: 3000 ,
421+ container: Container ,
402422 controllers: [__dirname + ' /controllers/*.js' ],
403423 middlewares: [__dirname + ' /middlewares/*.js' ],
404424});
0 commit comments