Reputation: 306
I'm developing a realtime chat/messenger app, sort of like Telegram or Discord. In my app, I need to fetch messages in realtime as they come in. Other apps (Discord is built on React Native) are able to show push notifications in realtime as messages come in. I'm wondering how I can do this as most packages for background tasks only allow it to run every few minutes at the least. I'd like to have a constant task running, using socket.io to get new messages. I would prefer to have Android and iOS support.
Thank you!
Upvotes: 2
Views: 2442
Reputation: 128
you cannot create a constant running background task unless you apply some sort of hack like playing silent audio in the background all the time to keep your app (especially in IOS) from being killed by OS
with that said, for a chatting app, you do not need a constant background task, all these apps when in background relay on the push notification
you can send data object along with notification and if the user interacts with the notification, that data can be received in the app .. and as soon as the app is foregrounded you fetch all the messages from socket (server) by any means.
Upvotes: 2
Reputation: 1118
For Push Notifications, you can take a look at this package: https://github.com/zo0r/react-native-push-notification.
For any other background tasks, take a look at Headless JS and workers such as https://facebook.github.io/react-native/docs/headless-js-android.html and https://github.com/devfd/react-native-workers.
Upvotes: 2