Reservoir api websocket api

63 views Asked by At

I'm trying to subscribe to reservoir websocket api https://docs.reservoir.tools/reference/websockets and I have created a custom react hook to subscribe & unsubscribe to some of the events

import {useCallback,useEffect,useState} from "react";

const API_KEY =  process.env.NEXT_PUBLIC_RESERVOIR_API_KEY ?? ''
const useReservoirWebSocket = () => {

    const [socket,setSocket] = useState<WebSocket|null>(null)

    useEffect(() => {

        const newSocket =  new WebSocket(`wss://ws.ws-sepolia.reservoir.tools`);

        newSocket.onopen = () => {
            console.log('Socket connected')
            setSocket(newSocket);

            newSocket.onmessage = (event) => {
                console.log('Message received: ', event.data);
            };
        }




        newSocket.onerror = (error) => {
            console.log('Connection error', error)
        }

        newSocket.onclose = () => {
            setSocket(null)
        }

        return () => {
            newSocket.close();
        }

    },[])


    const subscribe = useCallback((event:string, payload={}) => {
        if(socket) {
            socket?.send(JSON.stringify({
                type:'subscribe',
                event,
                ...payload
            }))
        }
    },[socket])

    const unsubscribe = useCallback((event:string,payload={}) => {
        if(socket) {
            socket?.send(JSON.stringify({type:'unsubscribe',event,...payload}))
        }
    },[socket])


    return {subscribe, unsubscribe, socket}

}

export default  useReservoirWebSocket

But the connection can never go through.

enter image description here

Any hint would be highly appreciated.

0

There are 0 answers