Socket IO
Anonymous
TRIALOP

2 years 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

34 Replies

Anonymous
TRIALOP

2 years ago

n/a


Anonymous
TRIALOP

2 years 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


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

anyone?


brody
EMPLOYEE

2 years 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


Anonymous
TRIALOP

2 years ago

so if i understood you correctly


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

this should work right?


brody
EMPLOYEE

2 years 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


brody
EMPLOYEE

2 years ago

though I can't comment on if the auth is correct


Anonymous
TRIALOP

2 years ago

it works if I use seperate instance for ws


Anonymous
TRIALOP

2 years ago

1228367213405147100


Anonymous
TRIALOP

2 years ago

but if I try to use the same instance for both https and wss it doesn't work


brody
EMPLOYEE

2 years ago

right but that's obviously not optimal


brody
EMPLOYEE

2 years ago

as mentioned previously, your websockets and http server need to listen on the same port


Anonymous
TRIALOP

2 years ago

okay but what is the solution?


brody
EMPLOYEE

2 years ago

having your websockets and http server listen on the same port


Anonymous
TRIALOP

2 years ago

they are on the same port already


brody
EMPLOYEE

2 years ago

what port specifically


Anonymous
TRIALOP

2 years ago

3000


brody
EMPLOYEE

2 years ago

it needs to be the PORT environment variable


Anonymous
TRIALOP

2 years ago

1228368134986272800


Anonymous
TRIALOP

2 years ago

that's the one


brody
EMPLOYEE

2 years ago

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


Anonymous
TRIALOP

2 years ago

bro I don't understand you


Anonymous
TRIALOP

2 years ago

can you please be more specific


Anonymous
TRIALOP

2 years ago

the PORT is defined as env variable


Anonymous
TRIALOP

2 years ago

what should I do? remove it from there or?


brody
EMPLOYEE

2 years 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


Anonymous
TRIALOP

2 years ago

got it thanks


Anonymous
TRIALOP

2 years ago

will try now without port


Anonymous
TRIALOP

2 years ago

thanks bro it works now


Anonymous
TRIALOP

2 years ago

<:salute:1137099685417451530>


brody
EMPLOYEE

2 years ago

no problem


Loading...