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

Not an issue - but react-use-websocket v3 works with React Native #27

Open
mr-ryan-james opened this issue Aug 22, 2022 · 6 comments
Open

Comments

@mr-ryan-james
Copy link

I just wanted to call this to anyone's attention who might find it useful - react-use-websocket (the repo this is forked from) v3 works fine with react-native, or at least it has been for me. The newest version (v4) depends on react-dom, so it doesn't work, but v3 seems to work really nicely.

https://github.com/robtaussig/react-use-websocket/releases/tag/3.0.0

@talksik
Copy link

talksik commented Sep 13, 2022

thanks @mr-ryan-james

@akanoce
Copy link

akanoce commented Apr 26, 2023

Unfortunately, react-use-websocket does not provide support for additional headers unlike this library.
Seems like User-Agent is not defined by default on iOS, potentially leading to issues in cases where this header is required (like 403 from the wss server)

@antgel
Copy link

antgel commented Oct 25, 2023

So as of October 2023 which library is generally preferred for websocket development with React Native (well, Expo)? Do we even need a library or can we just use the socket.io SDKs? The issues and PRs on this library seem to be basically ignored by the maintainer, whose only interaction in years has been merging Dependabot commits.

@ybupro
Copy link

ybupro commented Nov 9, 2023

So as of October 2023 which library is generally preferred for websocket development with React Native (well, Expo)? Do we even need a library or can we just use the socket.io SDKs? The issues and PRs on this library seem to be basically ignored by the maintainer, whose only interaction in years has been merging Dependabot commits.

Did you find any solution or new library? Thank you!

@antgel
Copy link

antgel commented Nov 10, 2023

Did you find any solution or new library? Thank you!

Not yet; I haven't had time to look as I've been working on other projects. I also guess this isn't the best place to ask, I might try Discord when I get back onto this project.

@mr-ryan-james
Copy link
Author

mr-ryan-james commented Dec 18, 2023

This obviously comes with no guarantees, but this solution seems to be working ok with me, feel free to adapt it to your own needs (for example, removing the auto reconnect logic)

import { useCallback, useEffect, useRef } from 'react';
import { config } from 'api/config';
import { useAppState } from '@react-native-community/hooks';

// your websocket url
const websocketUrl = config.websocketUrl;

const handleMessage = async (e: MessageEvent) => {
  // do something useful here
};

export default function useWebSocket() {
  let ws = useRef<WebSocket | undefined>();
  let currentAppState = useRef<string | undefined>();
  const appState = useAppState();

  const attemptConnection = useCallback(
    (count: number = 0) => {
      if (ws.current?.readyState === WebSocket.OPEN) {
        return;
      }

      ws.current = new WebSocket(websocketUrl);

      ws.current.onopen = () => console.log('ws opened');
      ws.current.onerror = (e) => console.log('ws error', e);
      ws.current.onclose = () => {
        console.log('ws closed', currentAppState);
        if (currentAppState.current === 'active' && count < 5) {
          attemptConnection(count + 1);
        }
      };

      ws.current.onmessage = handleMessage;
    },
    [],
  );

  useEffect(() => {
    currentAppState.current = appState;
    if (appState !== 'active') {
      console.log('ws close', appState);
      ws.current?.close();
      ws.current = undefined;
      return;
    }

    attemptConnection(0);

    return () => {
      ws.current?.close();
      ws.current = undefined;
    };
  }, [appState, attemptConnection]);
}

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