0.1.0 • Published 12 months ago

@ruso/nestjs-socket-client v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

NestJS Socket.IO Client

A minimal Socket-IO client wrapper for your NestJS applications.

Instalation

Package has to be installed with the socket.io-client v4+.

Via npm npm install @ruso/nestjs-socket-client socket.io-client

Via yarn yarn add @ruso/nestjs-socket-client socket.io-client

Usage

In your AppModule file:

import { Module } from '@nestjs/common';
import { SocketIoClientModule } from '@ruso/nestjs-socket-client';

@Module({
	imports: [
		SocketIoClientModule.forRoot({
			url: 'ws://localhost'
			// ... options for the client
			// https://socket.io/docs/v4/client-api/#new-Manager-url-options
		})
	]
})
export class AppModule {}

For asyncrounous configuration use forRootAsync():

import { Module } from '@nestjs/common';
import { SocketIoClientModule } from '@ruso/nestjs-socket-client';

@Module({
	imports: [
		SocketIoClientModule.forRootAsync({
			import: [ConfigModule],
			useFactory: (config: ConfigService) => ({
				url: config.get('SOCKET_IO_URL')
				// ... options for the client
				// https://socket.io/docs/v4/client-api/#new-Manager-url-options
			}),
			injectable: [ConfigService]
		})
	]
})
export class AppModule {}

And in your service:

import { Injectable, Inject, Logger } from '@nestjs/common';
import { SocketIoClientModule, OnSocketEvent, SOCKET_IO_CLIENT } from '@ruso/nestjs-socket-client';
import { Socket } from 'socket.io-client';

@Injectable()
export class Service {
	private logger = new Logger('Service');

	/**
	 * Inject the socket instance and use it's methods this way:
	 */
	constructor(@Inject(SOCKET_IO_CLIENT) private readonly socket: Socket) {}

	@OnSocketEvent('connect')
	onConnect() {
		this.logger.log('Successfully connected the socket.io server!');
	}
}

Thanks

This repository is an extended version of another project built by @danocmx, so special thanks to him for this contribution.

Author

Ruslan Gonzalez