Webhooks and Push Notifications

So far, every topic has been in Shopend’s broker. Publish/subscribe also shows up in two places outside it:

  • Webhooks
  • Push notifications

Webhooks

A merchant keeps its own warehouse system and wants to know when an order is placed. The merchant registers a URL with Shopend for placed orders. When an order is placed, Shopend sends an HTTP POST to that URL. The body of the request holds the order.

This is a webhook. The merchant subscribes by registering a URL, and Shopend publishes by sending a request to every URL registered for placed orders. Here, the publisher keeps the list of subscribers itself. Adding a subscriber adds a row to that list. The publisher’s code does not change.

Shopify and Stripe both work this way. Shopify’s topic for a new order is orders/create, and Stripe sends, for example, payment_intent.succeeded when a payment succeeds. Both send a POST with a JSON body to an HTTPS URL that the subscriber registered.

Push notifications

Mugshop has a phone app. Shoppers who want to hear about sales turn on sale alerts, and the app shows a notification when a sale starts. The notification appears even when the app is closed.

  • When a shopper turns on sale alerts, the app subscribes to a topic, mugshop-sales, with Google’s push service, Firebase Cloud Messaging (FCM).
  • When the flash sale starts, Mugshop sends one notification to the topic.
  • The push service delivers a copy to every phone subscribed to the topic. It keeps a connection open to each phone.

Here, the push service acts as the broker. Mugshop does not know which phones are subscribed, and the app does not poll. FCM’s documentation calls this topic messaging and describes it as based on the publish/subscribe model.

The push service is not Mugshop’s choice. A phone keeps one connection to one push service, shared by all its apps, because a connection for each app would cost battery and data. The company that makes the phone’s operating system runs that service. On an iPhone, it is Apple’s push service, the Apple Push Notification service (APNs). On Android, it is FCM, through Google’s services on the phone. FCM can also send to iPhones, but it hands those notifications to APNs. While a phone is offline, the push service keeps its notifications for a limited time. FCM keeps them for up to 28 days by default.

More often, an app’s server sends a notification to one phone. The app registers with the push service, gets a device token, and sends the token to the server. The server sends the notification with that token. That is a message to one receiver, not publish/subscribe.