robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.59k stars 135 forks source link

useWebSocket rendering #185

Open keene-dalveytech opened 1 year ago

keene-dalveytech commented 1 year ago

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
siriusye commented 1 year ago

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

weishaodaren commented 9 months ago

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,
    },
  });
mberdyshev commented 6 days ago

The similar issue is #123.