a year ago
Hello everyone I am facing an issue connecting my app with a railway server through sockets
I'd appreciate if somenone can help me
0 Replies
we are using websocket so as I understood I need to use same port for https and wss
socket gateway
import {
WebSocketGateway,
WebSocketServer,
OnGatewayConnection,
} from '@nestjs/websockets';
import { Server } from 'socket.io';
@WebSocketGateway({
cors: {
origin: '*',
},
})
export class SocketService implements OnGatewayConnection {
handleConnection(client: any, ...args: any[]) {
const token = client.handshake.auth.token;
client.join(token.toLocaleLowerCase());
}
@WebSocketServer()
server: Server;
sendMessage(wallet: string, message: any, event: string) {
this.server.to(wallet.toLocaleLowerCase()).emit(event, message);
}
}
weboscket provider
'use client'
import React, { FC, useContext, useEffect, useState } from 'react';
import { io } from 'socket.io-client';
import { useAccount } from 'wagmi';
const WebSocketContext = React.createContext<{ socket: any }>({} as any);
const WebSocketProvider: FC<{ children: React.ReactNode }> = ({ children }) => {
const [socket, setSocket] = useState();
const { address } = useAccount();
useEffect(() => {
if (address && !socket) {
const createdSocket = io(process.env.NEXT_PUBLIC_WSS_URL as string, {
auth: {
token: address.toString(),
},
});
setSocket(createdSocket);
}
return () => {
socket?.close();
};
}, [address]); // Empty dependency array to run only once on mount and unmount
return (
{children}
);
};
export const useWebSocket = () => {
return useContext(WebSocketContext);
};
export default WebSocketProvider;
if I set for NEXTPUBLICWSS_URL variable https - socket doesn't work
but If I set wss - socket works but https requests not
btw it works perfectly fine locally with different ports but when we deploy the app it doesn't
a year ago
use the same port for http and ws, your app should not be doing https or wss itself, as railway handles the secure part of those transports for you
const createdSocket = io("wss://hip-riddle-production.up.railway.app", { auth: { token: address.toString(), }, });
a year ago
that would be the correct URL your client would need to use, yes. that will work as long as your server code is done properly
a year ago
though I can't comment on if the auth is correct
but if I try to use the same instance for both https and wss it doesn't work
a year ago
right but that's obviously not optimal
a year ago
as mentioned previously, your websockets and http server need to listen on the same port
a year ago
having your websockets and http server listen on the same port
a year ago
what port specifically
a year ago
it needs to be the PORT environment variable
a year ago
you shouldn't be setting it yourself, your app should be listening on it by default
a year ago
as previously mentioned, you should not be setting the PORT environment variable yourself, your app just simply needs to listen to it as its set automatically for you by railway
a year ago
no problem