Skip to content

How to Create Multiple connections to different hosts? #257

Open
@oleksiiyenik

Description

@oleksiiyenik

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 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions