Socket IO

AnonymousTRIAL

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

AnonymousTRIAL

a year ago

n/a


AnonymousTRIAL

a year ago

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


AnonymousTRIAL

a year ago

btw it works perfectly fine locally with different ports but when we deploy the app it doesn't


AnonymousTRIAL

a year ago

anyone?


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


AnonymousTRIAL

a year ago

so if i understood you correctly


AnonymousTRIAL

a year ago

const createdSocket = io("wss://hip-riddle-production.up.railway.app", { auth: { token: address.toString(), }, });


AnonymousTRIAL

a year ago

this should work right?


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


AnonymousTRIAL

a year ago

it works if I use seperate instance for ws


AnonymousTRIAL

a year ago

1228367213405147100


AnonymousTRIAL

a year ago

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


AnonymousTRIAL

a year ago

okay but what is the solution?


a year ago

having your websockets and http server listen on the same port


AnonymousTRIAL

a year ago

they are on the same port already


a year ago

what port specifically


AnonymousTRIAL

a year ago

3000


a year ago

it needs to be the PORT environment variable


AnonymousTRIAL

a year ago

1228368134986272800


AnonymousTRIAL

a year ago

that's the one


a year ago

you shouldn't be setting it yourself, your app should be listening on it by default


AnonymousTRIAL

a year ago

bro I don't understand you


AnonymousTRIAL

a year ago

can you please be more specific


AnonymousTRIAL

a year ago

the PORT is defined as env variable


AnonymousTRIAL

a year ago

what should I do? remove it from there or?


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


AnonymousTRIAL

a year ago

got it thanks


AnonymousTRIAL

a year ago

will try now without port


AnonymousTRIAL

a year ago

thanks bro it works now


AnonymousTRIAL

a year ago

<:salute:1137099685417451530>


a year ago

no problem