pez
pez2mo ago

Tips on sending batch notifications to a large group of users at once?

Hi team, for context - I'm storing user ExpoPushTokens in my users table in Convex. I was reading some of the docs on cron jobs in Convex and was wondering what the recommended approach would be to send daily notifications to a large group of users at once (doesn't have to be at exactly the same time). I was thinking I could make a call to fetch the users table (though I'd probably wanna do this in a paginated manner so I don't fetch too many items at once), get the Expo push tokens that way, and then make batched calls to the Expo service using an internal action. Is this the recommended approach for a use case like this?
13 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
sshader
sshader2mo ago
Just checking that you've seen https://github.com/get-convex/expo-push-notifications Paginating sounds good here (probably by something like _creationTime so you can make sure you get all the users even if the data changes between fetching pages). Sounds like you already know that Expo has a batch API for sending push notifications which you want to use. One way to do this is with a mutation that fetches a page of data, kicks off the internal action, and then schedules itself to run again with a cursor
GitHub
GitHub - get-convex/expo-push-notifications
Contribute to get-convex/expo-push-notifications development by creating an account on GitHub.
pez
pezOP2mo ago
@sshader yep! saw this but since I already store the push tokens on the user side, decided I probably didn’t need to use the convex component. was going down the route of defining a “use node” internal action that used the expo-server-sdk for sending notifications in batches of 100 until users have been exhausted was going to run this action in a cron that ran daily - are scheduled functions necessary here or can the cron that loops over users in batches suffice? is there a time limit to actions/crons?
erquhart
erquhart2mo ago
The cron would kick off once per day, and while you could have it be a single long running action that pages through users, there's risk of it timing out. Having the action fetch a page, send notifications, and then reschedule itself with the cursor for the next page is a sturdier approach.
sshader
sshader2mo ago
Makes sense to not use the component if you already have tokens stored (but also migrating shouldn't be too hard if you ever want to), but it might be a good source of inspiration for sending batched notifications (I don't think I used expo-server-sdk in the component and just hit their API with a fetch, but idk what niceties the sdk provides) There aren't limits specific to crons, but an action can run at most 10 minutes. As erquhart said, if you do self scheduling functions, you'll never have to worry about the 10 min action limit. Yet another approach is to do self scheduling mutations, which load a batch, schedule an internal action to send the notifications, then schedules itself. Mutations get automatically and safely retried on failure, so you have a better shot of all your notifications getting delivered (vs. if one of your actions fails in one of the first few batches, you might fail to handle all the rest of the data)
Riki
Riki5w ago
@pez I know that might not be the answer you are looking for, but most likely you could do that more easily OneSignal (an alternative to expo push notif), that has a RN SDK. Basically: 1. Every user in your app is registered thanks to the onesignal sdk 2. Every day you launch a cron on convex that calls the onesignal api 3. OneSignal handles for you push messaging ALL your users in one shot
pez
pezOP5w ago
@erquhart @sshader these are awesome suggestions!! thanks for the help here. Will try scheduling actions/mutations @sshader so you’re saying call a mutation which itself calls an internal action to send notifs in batches of 100. then let the mutation schedule itself again with the next page of users? would definitely want to optimize for reliable delivery, so mutations might be the move here
jamwt
jamwt5w ago
I'd enqueue these batches in a workpool ( https://www.convex.dev/components/workpool ) for reliable retries etc.
Convex
Workpool
Workpools give critical tasks priority by organizing async operations into separate, customizable queues.
jamwt
jamwt5w ago
and to rate limit this background work in a systematic way
pez
pezOP4w ago
@jamwt super cool stuff. will give it a try!
Sohizi
Sohizi12h ago
Hello, I have a question related to https://github.com/get-convex/expo-push-notifications, I want to send push notification by batch of 100, how does it batch notifications? I didn't find any code implementation that illustrates it. Should I loop through each record and await for pushNotifications.sendPushNotification? Or should I await on Promise.all? Thanks
jamwt
jamwt12h ago
hi. yeah, you can just do it in a loop awaiting each one. it's a fair point that a batch api would be nice to avoid the calling overhead to the component. feel free to make a ticket on the repo so we can double back on it
Sohizi
Sohizi11h ago
Awesome! Thank you for your help.

Did you find this page helpful?