Libraries v5.2.0
    Preparing search index...

    Function useSubscription

    • React hook that connects to a WebSocket subscription and provides reactive state.

      Type Parameters

      • TOutgoing
      • TIncoming = never

      Parameters

      Returns UseSubscriptionState<TOutgoing, TIncoming>

      Reactive subscription state: lastEvent, events, state, send, close.

      function LiveFeed() {
      const { events, state, send } = useSubscription(
      () => client.live.events(),
      { maxEvents: 100 }
      );

      return (
      <div>
      <p>Status: {state}</p>
      {events.map((e, i) => <div key={i}>{e.message}</div>)}
      <button onClick={() => send({ text: 'hello' })}>Send</button>
      </div>
      );
      }