0

I am trying to run a unstoppable background service in react native which will continuously listen for messages via Socket.io from server.

For this I found several solutions with several problems

  1. Use FCM

    The problem with method is that first I have to listen for FCM event than connect to my server and get the data(Messages + images + Videos). And I don't want to use firebase for my app as its expensive and don't want to store my user data (Mainly images) and handle user auth.

  2. Use react-native-background-fetch

    This thing is pretty good but it only allow to run single job every 5 minutes and if your service is killed by android than you are gone

  3. Use Headless JS

    To be honest I cannot understand its documentation

Please tell me what to s I am confused and amazed that there is no proper way to handle this TIA regards

Salman Arshad
  • 343
  • 6
  • 23
  • 1
    What do you mean by "unstoppable background service"? Android doesn't allow such a thing, in order to prevent apps from draining battery and data. Best case, Anrdoid allows a foreground service, which requires a status notification, but it can still be killed by Android if it needs the memory. – Doug Stevenson Feb 04 '20 at 18:07
  • Got it. Then how come apps like facebook whatsapp snapchat receive notification without opening it. And how does FCM works? – Salman Arshad Feb 04 '20 at 18:15
  • Then what should I do to listen y text messages from server socket? – Salman Arshad Feb 04 '20 at 18:19
  • That's just push messaging. FCM does that. The plumbing for that is handled by services the device, both Android and iOS. – Doug Stevenson Feb 04 '20 at 18:29
  • Can you please tell me that how FCM exactly does that? – Salman Arshad Feb 04 '20 at 18:43
  • This has been discussed a lot already on stack overflow and other sites - do some searches. – Doug Stevenson Feb 04 '20 at 18:48

2 Answers2

1

Maybe you can implement a native module java/objective-c https://facebook.github.io/react-native/docs/native-modules-android

and run a background thread like this https://stackoverflow.com/a/31549559/5754159

Roberto Clavijo
  • 149
  • 1
  • 6
1

for that you need to learn native-modules and react-native-bridge and some basics of android and ios services. Here is the link for you reference.

https://medium.com/reactbrasil/how-to-create-an-unstoppable-service-in-react-native-using-headless-js-93656b6fd5d1