MongoDB 2022 Hackathon submission - Introducing Ghost Notifier
Tags: atlashackathon22, mongodb, atlas, typescript
Published: 2022-11-20 10:16:55

---
title: MongoDB 2022 Hackathon submission - Introducing Ghost Notifier
published: true
description: Send realtime notifications to your Ghost blog
tags: atlashackathon22,mongodb,atlas,typescript
---

## What I built
A realtime integration for the [Ghost blogging platform](https://ghost.org/). This includes:

- A backend HTTP service 
- An example frontend library for Ghost's 'Casper' - theme

As a bonus: My LaMetric clock was in the mail today 🎆. It can receive notifications about new and updated posts, so I integrated a dedicated code snippet in this submission. Check out the readme on Github on how to use it together with Ghost notifier. I'm also listing a brief showcase video under 'Additional Resources/Info'.

### Category Submission:
About real-time

### App Link
This service aims to be self-hosted. I'm providing a docker setup with Ghost + a step-by-step guide on registering the necessary webhooks as part of the readme on Github.

### Screenshots
Please see the [Github readme](https://github.com/tq-bit/ghost-notifier) for screenshots

### Description
Ghost-Notifier does two things.

**1. It receives a published article as input and stores a notification**
- Ghost-Notifier is a microservice built on top of MongoDB and Express.js. 
- It receives Ghost articles over HTTP and stores a subset of their data in MongoDB.

**2. It produces output when a DB change is detected and sends it to connected clients**
- Third-party clients can subscribe to Ghost-Notifier via. HTTP(S)
- When a publisher creates or updates a post, MongoDB triggers a notification using its built-in Change Streams
- This notification is sent via Server-Sent-Event to connected clients

### Link to Source Code
{% embed https://github.com/tq-bit/ghost-notifier %}

### Permissive License
MIT

## Background
I'm using Ghost CMS to write about web development. The platform's been a valuable asset in my toolkit for the last two years and continues to be so.

Compared to other blogging platforms, Ghost aims to be lightweight in design. It includes no classic plugins, but uses webhooks to trigger third party extensions to tweet, share or trigger workflows.

My blog does not receive enough traffic to add a lot of them. But if I ever attempted to scale, I'd look for more ways to keep users engaged. For instance, with notifications about new content.

Here's where MongoDB ChangeStreams seemed a perfect match for Ghost's webhooks.

### How I built it
Ghost-Notifier is a Typescript backend built on Express.js. It uses MongoDB Atlas to store a subset of post data on two events: 

1. New post published
2. Published post updated

These posts are stored and trigger MongoDB's `ChangeStream` functionality. 

Using MongoDB instead of in-mem DBs opens the gate to even more features:

- Use MongoDB's toolkit, including sharding, transactions and the query API to make scaling a child's play.
- It's easier to apply advanced analytics if you run Ghost-Notifier as SAAS <- this would be how I host this app as a product
- You can keep building on MongoDB's native driver to quickly implement further features, such as a URL-shortener or a search index

I also picked up Server-Sent-Events (SSE) along the way. In this case, they deliver notifications to connected clients whenever a DB entry is updated. I even wrote an article about implementing this technique because I found it to be a valuable alternative to Websockets

MongoDB and SSE play incredibly well together and were swiftly implemented. I'll definitely use this combination more often from now on for stateful realtime-applications.

### Additional Resources/Info

- Video for the LaMetric integration example: https://youtube.com/shorts/Mdwa-9BS4W4
- The article I wrote about Node.js + SSE: https://dev.to/tqbit/how-to-use-nodejs-for-server-sent-events-sse-5ggj
- MongoDB Change Streams docs: https://www.mongodb.com/docs/manual/changeStreams/
- MDN on server-sent-events: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
- MongoDB vs. Redis comparison: https://www.mongodb.com/compare/mongodb-vs-redis
- Ghost Webhooks docs: https://ghost.org/docs/webhooks/