Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alxshelepenok/inversify-socket-utils
Some utilities for the development of socket.io application with InversifyJS.
https://github.com/alxshelepenok/inversify-socket-utils
inversify inversion-of-control nodejs socket-io typescript websockets
Last synced: about 1 month ago
JSON representation
Some utilities for the development of socket.io application with InversifyJS.
- Host: GitHub
- URL: https://github.com/alxshelepenok/inversify-socket-utils
- Owner: alxshelepenok
- License: mit
- Created: 2018-07-23T20:48:50.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-09T19:25:10.000Z (about 2 months ago)
- Last Synced: 2024-12-10T05:35:18.357Z (about 2 months ago)
- Topics: inversify, inversion-of-control, nodejs, socket-io, typescript, websockets
- Language: TypeScript
- Homepage:
- Size: 5.17 MB
- Stars: 19
- Watchers: 6
- Forks: 7
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# inversify-socket-utils
Some utilities for the development of socket.io applications with Inversify.
## Installation
You can install `inversify-socket-utils` using npm:
```sh
npm install inversify inversify-socket-utils reflect-metadata --save
```Please refer to the [InversifyJS documentation](https://github.com/inversify/InversifyJS?tab=readme-ov-file#-installation) to learn more about the installation process.
## How to use
Step 1: Create a controller.
```ts
import "reflect-metadata";import { injectable } from "inversify";
import { Socket } from "socket.io";import {
connectedSocket,
controller,
onConnect,
onDisconnect,
onMessage,
} from "inversify-socket-utils";@injectable()
@controller("/namespace")
export class MessageController {
@onConnect("connection")
connection() {
console.log("client connected");
}@onDisconnect("disconnect")
disconnect() {
console.log("client disconnected");
}@onMessage("message")
message(@connectedSocket() socket: Socket) {
console.log("message received");
socket.emit("message", "hello!");
}
}
```Step 2: Configure container and server.
Configure the inversify container in your composition root as usual.
Then, pass the container to the InversifySocketServer constructor. This will allow it to register all controllers and their dependencies from your container and attach them to the socket.io app.
Then just call server.build() to prepare your app.In order for the InversifySocketServer to find your controllers, you must bind them to the `TYPE.Controller` service identifier and tag the binding with the controller's name.
```ts
import { createServer } from "http";
import { Container } from "inversify";
import { Server } from "socket.io";import { MessageController } from "./controllers/message";
import {
interfaces,
InversifySocketServer,
TYPE,
} from "inversify-socket-utils";const container = new Container();
container.bind(TYPE.Controller).to(MessageController);
const app = createServer();
const io = new Server(app, {
cors: {
origin: "*",
},
});const server = new InversifySocketServer(container, io);
server.build();app.listen(3000);
console.log("Server is listening on port 3000");
```## License
The MIT License (MIT)
Copyright (c) 2024 Alexander Shelepenok
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.