Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

you can turn off automatic connection beforehand #204

Open
xushnud123 opened this issue Sep 8, 2023 · 6 comments
Open

you can turn off automatic connection beforehand #204

xushnud123 opened this issue Sep 8, 2023 · 6 comments

Comments

@xushnud123
Copy link

i want to cancel automatic connection beforehand how can i do it

@ElectricCoffee
Copy link

There's a third parameter to the useWebSocket hook which isn't documented.

Setting it to false will prevent automatic connection and disconnect the websocket if it is already connected

@HarunKilic
Copy link

This should've been documented. I've been using hours to find a workaround for this :D

@ElectricCoffee
Copy link

This should've been documented. I've been using hours to find a workaround for this :D

I only found out about it because I had a feature request to add it in: #208

@robtaussig
Copy link
Owner

This should've been documented. I've been using hours to find a workaround for this :D

I’m sorry for your time! It is not entirely undocumented: https://github.com/robtaussig/react-use-websocket#interface

Do you have any thoughts regarding a better place for it?

@HarunKilic
Copy link

This should've been documented. I've been using hours to find a workaround for this :D

I’m sorry for your time! It is not entirely undocumented: https://github.com/robtaussig/react-use-websocket#interface

Do you have any thoughts regarding a better place for it?

I was looking for a section that would show how to disable it at initialization.

@markrickert
Copy link

markrickert commented Aug 12, 2024

I stumbled upon my solution to my react-native problem in this thread. I want the websocket to disconnect when the app is in the background and reconnect when the app is foregrounded. Some delay could be built into this too.

Here's how I solved it for my use case in react-native:

import { useAppState } from "@react-native-community/hooks"

// in your functional component:
const appState = useAppState()
const appIsActive = appState === "active"
const ws = useWebSocket(
  socketURL, 
  {
      shouldReconnect: () => appIsActive,
      // Other options
  },
  appIsActive, // boolean to indicate if the websocket should be connected or not.
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants