Search code examples
mysqlreal-time

How to generate notifications from MySQL table updates?


I have a full stack app that uses React, Node.js, Express, and MySQL. I want the react app to respond to database updates similar to Firebase: When data changes, I want a real-time notification sent to my app.

I want to use stock MySQL (no plugins), so that I can use AWS RDB or whatever.

I will use socket.io to push the real-time notifications to the web app.

To avoid off-target responses, I'll summarize various approaches that are not what I am looking for:

  1. The server could poll, or each client could poll. (Not real-time, but included for completeness. When I search, polling is the only solution I find.)

  2. Write a wrapper that handles all MySQL updates, handles subscriptions, and sends the notifications. This is a complicated component that adds complexity. Firebase is popular because it both increases performance and reduces complexity. I like Firebase a lot but want to do the same thing with MySQL.

  3. Use Firebase to handle the real-time notifications. The MySQL wrapper could use Firebase to handle the subscriptions and notifications, but there is still the problem of triggering the notifications in the first place. Also, I don't want to use Firebase. (For example, my application needs to run in an air-gapped environment.)

The question: Using a stock MySQL database, when a table changes, can a notification server discover the change in real-time (no polling), so that it can send notifications?


Solution

  • The approach that works is to listen to the binary logs. This way, any change to the database will be communicated in real-time. The consumer of the binary logs can then publish this information in a number of ways. A common choice is to feed a stream of events to Apache Kafka.

    Debezium, Maxwell, and NiFi work this way.