websocket connect issues in my railway deployed app
rf92
PROOP

4 months ago

I've a Nest.js app that is working fine on local and Docker, but when I deploy it to Railway, it gives me an error.
I've tried every suggestion from AI to resolve it, but it's not working

I've tried using the internal address, and right now I've set public URL of the backend to connect to it, but still I'm seeing this error

WebSocket connection to 'wss://{backend-url}.up.railway.app/socket.io/?EIO=4&transport=websocket'

the error redirects me to this part:

class X extends K { createSocket(t, e, s) { return $ ? new W(t,e,s) : e ? new W(t,e) : new W(t) } doWrite(t, e) { this.ws.send(e) } }

Can you help me solve it?

$10 Bounty

5 Replies

mikaoelitiana
HOBBY

4 months ago

Hi @Aref , it may be related to CORS. Can you try to add something like

@WebSocketGateway({
  cors: {
    origin: '{backend-url}.up.railway.app'
  },
})

rf92
PROOP

4 months ago

I have origin in env file

FRONTEND_URL="https://{frontend}.up.railway.app"

do i need to backend here as well?

`const websocketOrigins = getAllowedOrigins();
const resolvedOrigins =
websocketOrigins.length > 0 ? websocketOrigins : ['http://localhost:3000'];

@WebSocketGateway({
cors: {
origin: resolvedOrigins,
credentials: true,
},
namespace: '/',
transports: ['websocket', 'polling'],
})
@UseGuards(WsJwtGuard)
export class EventsGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
{`


rf92
PROOP

4 months ago

I've added backend url here as well but still same error


mikaoelitiana
HOBBY

4 months ago

Can you try origin: "*" just to see if the issue is coming from there ?
What do you see in your browser network tab with Socket filter ?

1436491874650292200


rf92
PROOP

4 months ago

Still same error after applying origin : "*"

1436498114449244200


Loading...