NestJS Rest API + Socket.IO port expose

I have a NestJS REST API service, which I have been working on for some time, for which the need recently arose to implement a socket output, initially in the same project, in the future it will be separated into more microservices.

For which a doubt arises, if railway decides on which port the REST API comes out, but to instantiate SocketIO, a different port must be instantiated, is there a way to expose both ports? the one exposed by default for the rest api, and release the extra port that was defined for Socket.IO?Example of the Socket service

@WebSocketGateway(5050, {cors: {origin: '*'}, namespace: 'board'})
export class BoardGateway {
  @WebSocketServer()
  server: Server;

  @SubscribeMessage('events')
  findAll(@MessageBody() data: any): Observable<WsResponse<number>> {
    console.log(data);
    return from([ 1, 2, 3 ]).pipe(map(item => ({event: 'events', data: item})));
  }

  @SubscribeMessage('identity')
  async identity(@MessageBody() data: number): Promise<number> {
    return data;
  }
}

Which instances port 5050.

Any help will be appreciate :)

5 Replies

a year ago

You can not expose multiple ports, your websocket server would need to utilise the same listener as your API server so that they can be ran on the same port.


Thank you Brody, it has worked for me, in addition to the above, I would like to ask about the automatic Api Gateway connections that you have, since it is very common for my deploys to crash when not finding a route to either postgres or redis, how could I solve it?This is for Redis

```

Error: getaddrinfo ENOTFOUND redis.railway.internal

```

and this for postgres

ERROR [ExceptionHandler] getaddrinfo ENOTFOUND postgres.railway.internal

a year ago

I'm not sure what you mean by "automatic gateway connections" there's no such concept of that on Railway.

For private networking domains to resolve correctly the services all need to be in the same environment.

There's also a few other caveats, but to continue, please tell me if your service has a volume on it.


All services are in the same environment, reference image attached

Attachments


a year ago

Try switching to the v2 runtime in the service settings for your wwt-auth service.