UNPKG

@rxap/nest-socket-io-client

Version:

This package provides a NestJS module for integrating a Socket.IO client. It offers a client proxy service and a provider for managing the Socket.IO client connection, allowing NestJS applications to easily interact with Socket.IO servers.

53 lines (52 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocketIoClientStrategy = void 0; const microservices_1 = require("@nestjs/microservices"); const common_1 = require("@nestjs/common"); /** * SocketIoClientStrategy is a custom transport strategy that allows * a Socket.IO client to communicate with the server. It extends the Server class * and implements the CustomTransportStrategy interface. */ class SocketIoClientStrategy extends microservices_1.Server { constructor(client, logger) { super(); this.client = client; this.logger = logger; } unwrap() { throw new common_1.NotImplementedException(); } on() { throw new common_1.NotImplementedException(); } /** * This method is triggered when you run "app.listen()". */ listen(callback) { this.client.on('connection', () => { this.logger.log('connection', 'SocketIoClientStrategy'); }); this.client.on('error', (error) => { if (error && typeof error === 'object' && error.message) { this.logger.error(error.message, 'SocketIoClientStrategy'); } else { this.logger.error(error); } }); this.messageHandlers.forEach((handler, pattern) => { this.client.on(pattern, (data) => { handler(data, this.client); }); }); callback(); } /** * This method is triggered on application shutdown. */ close() { this.client.disconnect(); } } exports.SocketIoClientStrategy = SocketIoClientStrategy;