flemadap
flemadap

Reputation: 699

TCP Server using NestJS

Is it possible to create a TCP server using NestJS??

I have a GPS tracker that communicate only with TCP. As NestJS could communicate between microservices with TCP, I thought that maybe NestJS could be used as low level network applications (something like java-netty/python-tornado)

Upvotes: 5

Views: 6108

Answers (1)

Mohsen Esmaeili
Mohsen Esmaeili

Reputation: 368

You could easily add a TCP server service as a provider for your NestJs module.

You can test it by "telnet 127.0.0.1 1337"

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import * as net from 'net';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'TcpServerService',
      useFactory: () => {
        const server = net.createServer(socket => {
          socket.write('\r\rFrom NestJs TcpServerService\r\n');
          socket.pipe(socket);
        });

        server.listen(1337, '127.0.0.1');

        return server;
      },
    },
  ],
})
export class AppModule {}

Upvotes: 8

Related Questions