mfrachet/server-push-hooks

Feature Request: Provide a way to delay the subscription

iagobruno opened this issue · 4 comments

Current behavior:

Currently, as soon as the component is mounted, the hook subscribes to receive events.

Proposal:

Add a way to manually enable the subscription when you want. Example:

import { useLazySocket } from 'use-socketio'

function Component(props) {
  const { subscribe, isListening } = useLazySocket('tweet', (newTweet) => {
    // ...
  })

  return <>
    <div>Is enabled: {isListening ? 'yes' : 'no'}</div>
    <button onClick={subscribe}>See new tweets</button>
  </>
}

I was inspired by the Apollo hooks api to make this draft.

Use case examples:

  • Wait for some information required in callback function be returned from server or external service.
  • Wait for a specific state in redux.
  • The component may receive events but for some reason (bad internet connection, mobile device or user not logged in) you don't want to enable it.

If you approve this idea, I will make the pull request with the implementation.

Closing because of duplication 😊

Where is the duplicate issue? I searched before creating this one.

It seems that for some reasons, this specific issue has been duplicated here #16