pocketbase / js-sdk

PocketBase JavaScript SDK
https://www.npmjs.com/package/pocketbase
MIT License
2.17k stars 127 forks source link

Error "EventSource connect took too long" in connecting in real time with react native + expo #257

Closed wesleywcr closed 1 year ago

wesleywcr commented 1 year ago

I'm trying to make a real-time connection in a chat on my app. But I'm facing this problem with the connection:

Error Subscribe: [Error: EventSource connect took too long.] My code:

 useFocusEffect(
    useCallback(() => {
      const fetchNewMessages = async () => {
        try {
          await pb.collection('messages').subscribe('*', (e) => {
            console.log('action', e.action);
            refetch();
          });
        } catch (error: any) {
          console.error('Error Subscribe', error.originalError);
        }
      };

      fetchNewMessages();

      return async () => {
        try {
          await pb.collection('messages').unsubscribe('*');
        } catch (error: any) {
          console.log('Error Unsubscribed', error.originalError);
        }
      };
    }, [refetch]),
  );

What can I do to make the real time functionality work correctly?

ganigeorgiev commented 1 year ago

I'm not sure how to help you debug it but I don't think the issue is JS SDK related.

Double check that client can access the PocketBase server.

Does regular requests works?

Where the above code is being executed - browser, servers-side, React Native?

I'll close the issue for now as there is no actionable item, but feel free to provide more details about your setup and ideally some minimal steps to reproduce, and I'll have a look.

ganigeorgiev commented 1 year ago

On second read, I missed that in the title is mentioned React Native.

In this case, you need a EventSource polyfill, for example react-native-sse:

import eventsource from "react-native-sse";

global.EventSource = eventsource;
ganigeorgiev commented 1 year ago

You could also check the comment in https://github.com/pocketbase/pocketbase/discussions/2484#discussioncomment-6118523.

wesleywcr commented 1 year ago

I've already added react-native-sse, others requests in pocketbase work normally. Only the question of the real time that generates the above error. Do I need to add another configuration to my project?

ganigeorgiev commented 1 year ago

@wesleywcr Please check the linked resources in https://github.com/pocketbase/pocketbase/discussions/2484#discussioncomment-6118523.

If that doesn't help, please provide more details how you are testing and ideally create a minimal reproducible repo and I'll investigate it further.

wesleywcr commented 1 year ago

It seems like it was a problem in the expo version. I was using SDK 49, I downgraded it to SDK 48 and worked normally.