Firebase concurrency read/write - node.js

I have a Firebase mobile application that serves many users.
The application requires to send email notifications to users. Firebase cannot send functional email.
Zapier is not an option as the webhook service is very limited and cant consume complex JSON such as the email body.
To solve this, I store the “email job” in the Firebase database (include To, subject and body”), I setup a “mail server” using nodejs server (at home) that listen to the Firebase database, so whenever there is a “new email job” it sends the mail and mark the job status to “DONE”.
In order to maintain high availability and scalability, I must be able to run more than one “mail servers” but this will cause duplication mail as all servers will listen to jobs.
I cannot address a job to a specific server, as the server may be down and I will lose jobs. Also, Firebase does not have kind of SELECT FOR UPDATE as SQL databases have to maintain concurrency.
Is there a way to solve this issue using Firebase? If no, any workaround?

Related

FCM, send multiple devices without tokens?

I want to send FCM to everyone who installed the app. Is it essential to get everyone's tokens from the database every time?
My app is using firebase firestore overall. If there are 100,000 users,
do I have to read 100,000 from database to send fcm each time? (I think it`s little heavy stuff isn`t it?)
another workroad exists?
I wonder Is the only way to send it by putting it in the registration ID?
And can you send it on time? All apps on the market send push messages on time, but if you read 100,000 and send fcm separately, shouldn't it arrive like this at 9:01 or 9:02? But why do I always get messages at 9 o'clock?
What are the methods, logic, algorithms they use (the way companies usually use)
I still have no clue at all.
There is no "send to all users" operation in FCM. You either will have to send to each token (that's not a heave operation for FCM, which handles billions of such calls every second), or you have to subscribe all instances to a specific topic and then send to that topics (which ends up the same behind the scenes, just with Firebase loading the tokens for the topic for you).
This has been covered a few times before, so I recommend checking:
How do you send a Firebase Notification to all devices via CURL?
How to send notifications to all devices using Firebase Cloud Messaging
Firebase Cloud Messaging - Send message to all users
The notifications panel in the Firebase console has an option to deliver messages at a specific time, but no such option exists in the Firebase Cloud Messaging API. You'll have to either implement your own mechanism to schedule the delivery, or you can deliver a data message right away and then only display the notification on the device when it's time.
This also has been covered a few times before, so check:
Firebase Messaging FCM Distribution over configurable time interval
How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?
Flutter Firebase Messaging: How to send push notifications to users at specified time

Howto email notifications from your web application

What is the correct way to setup mail notifications from your web app in the cloud era? Previously I always had some SMTP server, which I could use for that purpose, but this time my company mail is hosted and there is a limit of 500 mails per day sent from certain address in this system, so with rather high chances I can hit that limit. My first thought was to use services like sendpulse or mailchimp, but it seems that all these services are built having mass marketing campaigns in mind and I'm concerned about security in case of sensitive emails, like password restore or details about orders. I spent several hours searching for solution in the web, but did not find anything reasonable.
Ideally the service I'm looking for would guarantee, that content of my messages would never be stored or shown to anyone, besides email this service might also be able to send SMS (for password restore case) and have integration with messengers.

Should I use Azure Service (such as Scheduler) for sending rest messages to my bot, or use a separate thread for notifications?

I am creating a bot using Microsoft Bot Framework (BotBuilder) and want it to message the user when an appointment is about to begin.
I currently use Microsoft Graph api to access the user's Office 365 calendar and store the appointments. A background thread then keeps track of time and then messages the user when an appointment is about to start.
The current idea is to use Graph webhooks to notify my bot about new appointments.
My question is, would it be smarter to use an Azure service (such as Scheduler) to keep track of the appointments, and send rest messages to my bot, which will then send a message to the user?
My worry is, that as the amount of users rise, the amount of appointments and time checks will become too large, and that maybe Azure services would be able to handle it better.
This is a perfect fit for Azure Functions with a HTTP Trigger.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook
This article explains how to configure and work with HTTP triggers and bindings in Azure Functions. With these, you can use Azure Functions to build serverless APIs and respond to webhooks.
Azure Functions provides the following bindings:
An HTTP trigger lets you invoke a function with an HTTP request. This can be customized to respond to webhooks.
An HTTP output binding allows you to respond to the request.

How can I setup an Outlook notification webhook on Azure?

Currently we have subscription in Office 365. All users has access to Outlook emails. We are planning to build a batch job using Daemon or Server application (as described here https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api). The reason for the batch job to be as Daemon service is because we wanted to access all user emails, filter and forward to another mail server with one app identity.
Question 1 : How can we grant an app in Azure to access all users emails ?
As an alternative we could also build a webhook that is subscribed to inbound/outbound emails and once we get a notification in our system we can filter/forward to another mail server. https://dev.outlook.com/restapi/concepts/webhooks
Instead of building a job and calling a REST API, is there a way to
register a webhook in Azure for all users at once and redirect the
url to our system ?
That will save us from building a batch job that has to run everyday to grab all incoming emails and then filter. If we can integrate a webhook all we have to build is just the filter/forward part.
The Webhooks are only supported for user level credentials. i.e Each user has to give the app a permission to request data on behalf of users. However, in our case, we need to get notifications for all user emails to reach our application and this is impossible to do at the time of writing (Sep 2016).
The solution we went forward is to build a batch job to pull all data. An Admin has to give an access to the application (the application will have its own identity) and make subsequent requests. For the app to identify itself, we have used X.509 certificate
We followed this documentation and this
If you want to redirect the specific messages with Office 365, a easy way is config the mail flow on the Exchange Admin Center.
For example, we can config a rule to redirect to the messages to the mailbox you wanted as below:
Here is the document for the mail flow rules for your reference.

Service Worker Push Notifications with Angular2

I'm trying to piece together the general workflow of giving a user push notifications via the service worker.
I have followed this Google Developers service worker push notifications tutorial and am currently thinking about how I can implement this sort of thing in a small user based web app for experimentation.
In my mind, the general workflow of an web app supporting push notifications is as follows:
Client visits app
Service worker yields a push notification endpoint
Client sends the endpoint to the server
Server associates the endpoint with the current user that the endpoint was generated for
Every time something that your app would say is notification worthy happens, the server grabs the push notification endpoint(s) associated with the user, and hits it to send a push notification to any user devices (possibly with a data payload in Chrome 50+, etc)
Basically I just want to confirm that my general implementation thoughts with this technology are accurate, else get feedback if I am missing something.
You are pretty much bang on, there are some specifics that aren't quite right (but this is largely phrasing and may be done to personally taste).
Client visits app
Register a Service Worker that you want to use for push messaging
Use the service worker registration to subscribe the user to push messaging, at which point the user agent will configure an endpoint + additional values for encrypting payloads (If the the user agent supports it).
Client sends the endpoint to the server
Server store the the endpoint and data for later use (The server can associate the endpoint with the current user if the server if the web app has user accounts).
When ever the server wishes to send a notification to a user(s), it grabs the appropriate endpoints and calls them that will wake up the service worker which can then display a notification.
Payload support in coming in Chrome 50+ and at the time of writing payload is support in Firefox, but there are 3 different versions of encryption used for the payloads in 3 different versions of Firefox, so I'd wait for the payload support story to be ironed out a little before using it / relying on it.

Resources