Keeping application alive in background

Hi, I am using react-native-webrtc plugin to make a conference app and it works pretty good in foreground (when the app is active) but in background after a while when there is a connection problem which app state fails or disconnects there has to be a reconnect attempt which done by web sockets ( clients but when app goes background web socket connection is killed and disconnected so because of that participants cant reconnect to each other while app running in background.
I have followed the suggestion of @saghul using foreground service in here but this only helps to keep webrtc connection awake longer than 30 mins but if there is a connection problem occurs there is no way to connect users back , user has to activate application back to connect back and same loop starts if he goes background web socket connection will die again. How can I keep websocket connection alive when app goes background ? thanks


It’s possible socket.ios is using timers to send keepalive messages. setTimeout doesn’t work in the background by default, but you can use to monkey-patch setTimeout / setInterval and that should work.

But even if I patch the set timeout or set interval functions , if app is killing the connection new connection try is gonna be killed too isn’t it. How you guys handle reconnection in meet jitsi !

Connections shoulodn’t be killed if you enable voip and audio background modes. They may get killed but you should be able to reccreate them. We use XMPP BOSH, which is not long-lived, they are small requests, basically long polling.

I have tried reconnecting using background-timer but connection gets killed again after some seconds. I guess I have to run web socket connection as a foreground service some how. Any idea ? i think XMPP works because its not a keep alive web socket type connection its a new request every time. I tried only polling as transport type in socket io but it didn’t work also user is not responding to reconnect requests listeners doesn’t work

I have patched the ping functions of it keeps the connection alive but it doesn’t respond to events. Seems like listeners doesn’t work

I have followed the same suggestions here. In my case, the connection is fine, but the webrtc ice connection gets disconnected. Specifically, the iceconnectionstate change event is fire and goes from connected -> disconnected with 30s. This is while a foreground service is running. Any thoughts on why this would be the case?

I honestly don’t know.

No worries, will keep digging. I noticed the Jitsi app successfully keeps it’s connection alive using the same mechanism. Will keep investigating why the behavior differs and share any findings

How do you use socket io and when do you start foreground service right away after lunching the application or when there is a webrtc connection. I start when user joins a conference. without patching the socket io ping functions socket disconnects in my app. Does it keep connection alive without starting a foreground service too ?

I have tried my app in two device one has android 6 and one has android 9 and both kept webrtc connection alive for long time with foreground service. Did you try your app in a real device ?

I have been testing on the emulator, since I’ve notice other apps are able to keep their connections alive (ex: Discord). Trying on a device may be worthwhile, but mine running Android 8, so wasn’t sure if I should trust the behavior (vs Android 10/9 in the emulators).

I start the foreground service when the app enters the background. Perhaps, I should start it right away. Did that make a difference for you?

As for, I use it to get live content updates. I am able to keep that connection alive using a background-timer that periodically hits an endpoint on the same server the socket is connected

Well I just tried running foreground service when user joins a conversation which is a call. And I realized my problem was using async await in socket io listener when app goes background await keyword stops the execution of after it comes so my signalling function wasn’t get called.

Interesting… I’ll see if that’s relevant in my case. Would you mind sharing the snippet your foreground service code for android? Curious to see if there’s any differences there

I have just used the way its described in github repo;

if (Platform.Version >= 26) {
        const channelConfig = {
            id: 'notification_channel',
            name: 'VoIP',
            description: 'VoIP',
            enableVibration: false

    const notificationConfig = {
        id: 3456,
        title: 'App is running',
        text: 'The app is using VoIP features',
        icon: 'ic_icon'

    if (Platform.Version >= 26) {
        notificationConfig.channelId = 'notification_channel'

    try {
        await VIForegroundService.startService(notificationConfig);
    } catch (e) {

Hi, i am using foreground service. But it is still disconnecting. What does patching of socket io mean?

Its means changing the setTimeout,clearTimeout,setInterval etc functions of socket io with BackgroundTimer.setTimeout from react-native-background-timer module

If you upgrade to socketio v3, this should fix the timeout disconnection issues. In v3, the interval timers have been moved from the client side to the server, so they are much more reliable. Patching the client shouldn’t be necessary anymore.

I’m not sure if this solution works with Janus. I’m using MediaSoup and socketio v3 + foreground service works great on Android.

1 Like

I managed to sustain the connection when app goes to background. I am not using but using firebase for real time data and as a signaling server.
Its easy, free and most importantly, it works!

How did you end up doing that @smash-96 ?