react-use-websocket icon indicating copy to clipboard operation
react-use-websocket copied to clipboard

useWebSocket rendering

Open keene-dalveytech opened this issue 2 years ago • 3 comments

i save my data in other js file, so i don't want rerender. how should i do? thanks

const [, setMarketsAtom] = useRecoilState(marketsAtom) // from recoil store ts file
  const [marketSelection, setMarketSelection] = useRecoilState(marketSelectionAtom)

  const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket('wss://stream.binance.com:9443/stream', {
    share: true,
    filter: () => false,
  })

useEffect(() => {
    ...
    sendJsonMessage({ id: 1, method: 'SUBSCRIBE', params })
    
  }, [markets])

  console.log('useMarketDataSocket: render') //  always render
  
  

keene-dalveytech avatar Mar 28 '23 08:03 keene-dalveytech

I am facing the same problem, we need an option to stop the re-rendering, sometimes it could be problematic for component life circle management

siriusye avatar Jul 20 '23 09:07 siriusye

if use heartbeat, and always render, maybe should use filter like this:

 const { sendJsonMessage, lastMessage, readyState } = useWebSocketIO(socketUrl, {
    queryParams: { token },
    onMessage: patchMessage,
    shouldReconnect: () => mounted.current === false,
    reconnectAttempts: 5,
    reconnectInterval: 1000 * 5,
    filter: event => event.data !== '"pong"',
    heartbeat: {
      message: 'ping',
      returnMessage: 'pong',
      timeout: 1000 * 60 * 5,
      interval: 1000 * 5,
    },
  });

weishaodaren avatar Dec 30 '23 05:12 weishaodaren