Skip to content

Fix for Appstate changes causing disconnect #65

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

Conversation

ranjandsingh
Copy link

Closes #57

@EmilJunker
Copy link
Contributor

Instead of implementing this in the library itself, creating a dependency on another package ('react-native'), adding yet another config option, and making it unnecessarily complicated to debug, I think it would be better to just add instructions to the readme how one can stop and restart the SSE connection when the app switches between background and foreground state.

I think something like this should do the trick:

import { useEffect } from 'react';
import { AppState } from 'react-native';

useEffect(() => {
    const appStateSubscription = AppState.addEventListener('change', (nextAppState) => {
        if (nextAppState === 'active') {
            // App became active, reconnect SSE
            es.open();
        } else if (nextAppState === 'background' || nextAppState === 'inactive') {
            // App went to background, close SSE connection
            es.close();
        }
    });

    return () => appStateSubscription.remove();
}, []);

@wojciechkrol
Copy link
Collaborator

@ranjandsingh @EmilJunker Thank you for pointing out this issue. I have added instructions to the README on how to address it, following @EmilJunker's recommendation. I will not merge this PR to avoid adding unnecessary dependencies.

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

Successfully merging this pull request may close these issues.

does not establish connection after app is actived from sleep mode
3 participants