xsephtion
xsephtion

Reputation: 193

How to handle offline messages in React-Native using NodeJS and SocketIO

I am currently using SocketIO and NodeJS to handle messages. However the problem is that when the user becomes offline there's no way that the other user will receive the message.

The solution I came up with was to store the message in the database.

But a new problem arise, when fetching the message and push notification.

If I do fetch for "n" minutes in the server when the app is in background/inactive. There will be a lot of request in the server, and I personally think that it is inefficient. and also it drains the battery.

What is the proper way how to handle fetching the messages from database or pushing notification in app without making too much request in "n" minutes and draining too much power?

Upvotes: 1

Views: 1414

Answers (3)

xsephtion
xsephtion

Reputation: 193

Going back to this question if somebody stumbled upon this question.

The best way to handle offline messages regardless if you are using NodeJS/MongoDB, etc. is to store it on server's database. Then call an API that fetches the the messages which is equal to user's ID whenever the mobile app comes to foreground.

If your problem is that you needed notification and you are using

react-native-push-notifications / react-native-push-notification-ios

Then you should use the data notification to include the message on notification parameter on the server's side(Assuming that you are using Firebase Cloud Messaging). With this way you can directly save the message on the mobile's database.

Upvotes: 0

ambegossi
ambegossi

Reputation: 221

My suggestion is to implement a system of background jobs in the API, checking when there is a new notification to be launched, or with the notification already ready waiting to be launched in the queue. You can search for queue manager like Bull, Bee-Queue.

To launch push notification in the closed/inactive app, you can use a service like OneSignal or Firebase.

I implemented this a few weeks ago and did it this way. API = Node.js, Bull Queue App = React Native, OneSignal

Upvotes: 0

Rajan
Rajan

Reputation: 1568

You need to save the last sync time in the App. And whenever app comes from background/Inactive state. You need to call an API with this time. This API will give you all the messages and the Push notification which has comes after the last sync time. In this way, With one API call, you will be able to get all the messages and push notifications. I had used this approach to syncing my data in one of my app.

Upvotes: 1

Related Questions