ceessay
ceessay

Reputation: 332

How to listen to server side events in React Native?

I am setting up server-sent events on my Expo React Native App via react-native-event-source library .

With the function below, I should have data logged every 10 seconds. But nothing is printed in the console. Am I missing something?

  getTodayMissions() {
    ...
    const options = { headers: { Authorization: `Bearer ${token}` } };
    const eventsurl = `${BASE_URL}/server-events/`;
    const eventSource = new RNEventSource(eventsurl, options);
    const url = `missionstodayapp?userId=${user.id}`;


    console.log('start the watch', eventsurl, url);
    this.eventSource.addEventListener(url, (data) => {
      console.log('started the watch', data);
    });
  }

Upvotes: 1

Views: 3287

Answers (1)

If you are using android studio emulator, you should use 10.0.0.2 instead of localhost or 127.0.0.1 for your base URL.

Upvotes: 2

Related Questions