loading...

Is it possible to do realtime messaging on the JAMstack?

shortdiv profile image Divya ・2 min read

Real-time feedback can positively influence the overall user experience of a website. When a user requests help or time sensitive information (like realtime bus schedules or stock market prices), providing a timely response keeps them engaged and up to date with critical information. To achieve this experience with as little latency as possible, websites employ a system where data is streamed or pushed to users via delivery mechanisms such as Publish/Subscribe. The general concept here is real-time messaging where data is pushed as fast as possible to facilitate the distribution and delivery of messages. By design, real-time messaging is built to be consumed or used literally in real-time or as events occur. In the context of static sites on the JAMstack, achieving real time messaging via a Pub/Sub pattern can be a challenge. To receive a message, the receiving application needs to be proactive and attentive to events as they happen. For this to work, a long lived connection needs to be established so that the publisher can properly notify subscribers of incoming changes. In a JAMstack setup where content is pre-rendered, updating data means having to rebuild the site and invalidate the CDN cache. Rebuilding a site takes time, and there’s often a chance that data is stale by the time a site build is complete.

Though the JAMstack doesn’t lend itself well to patterns like Pub/Sub, accomplishing real-time messaging is possible within a JAMstack model. Web hooks are one way that this can be done. With webhooks, a web service can provide other services with real time information without the need for a constant connection. Compared to the observer model, webhooks are a simple way to accomplish event driven workflows like “If this then that” and applications can easily communicate when something new happens. Though publishing updates to a site still requires an entire rebuild, end users can be notified of changes through other means like Slack notifications, text messages. To be notified of new form submissions for instance, webhooks can be used to trigger notifications through the delivery mechanism of your choosing. In the event that a webhook is unavailable, Zapier offers a slew of service integrations so you can easily glue services together at the click of a button.

Compared to traditional approaches to realtime messaging where information is “pulled" or “polled”, realtime messaging on the JAMstack has to be event driven to work. It requires a service to explicitly notify a site of incoming changes. For more on accomplishing event driven workflows on the JAMstack, check out the Netlify documentation on outgoing webhooks

Posted on by:

shortdiv profile

Divya

@shortdiv

Tea drinker in a coffee drinking world

Discussion

markdown guide