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

How to Create Multiple connections to different hosts? #257

Open
oleksiiyenik opened this issue Dec 10, 2024 · 0 comments
Open

How to Create Multiple connections to different hosts? #257

oleksiiyenik opened this issue Dec 10, 2024 · 0 comments

Comments

@oleksiiyenik
Copy link

Hi, I hope this is not a duplicate, I didn't find anything related to it though.
I need to have two different connections and send data to them simultaneously.

I tried to do that by useMemo but eventually react Hooks I can't use inside other methods ;)

const connections = useMemo(() => {
    return socketArray.map((socketURL) => {
      const { sendMessage, lastMessage, readyState } = useWebSocket(socketURL, {
        onOpen: () => console.log(`Connected to ${socketURL}`),
        onClose: () => console.log(`Closed connection to ${socketURL}`),
        onError: (error) => console.error(`WebSocket error on ${socketURL}`, error),
      });

      return { sendMessage, lastMessage, readyState, socketURL };
    });
  }, [socketArray]);

I tried to create a custom hook, but the same result..

Thanks in advance for your feedback 🙏

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

1 participant