0

I am quite new to NodeJS but I've written a few apps (like a chat, real time page updater etc) And I've used mysql to read from my database and emit() information to my webpage, but how can I have nodeJS watch a database table and emit() whenever a row is added?

I have no idea where to start and google didn't produce much results. However there must be an include I can require() that will watch the database in someway.

Chud37
  • 4,907
  • 13
  • 64
  • 116

1 Answers1

2

The answer really isn't a nodejs question, but a mysql question. If mysql itself can tell you ( via an event or log ) that a row was added, nodejs could read and consume that data. Unfortunately, that doesn't seem to be possible, based on this answer. The answer suggests that the only thing you can do with mysql is to poll for new rows.

IF you're in control of inserting the data rows from nodejs itself, there shouldn't be any problem emitting those events after you get the confirmation it wrote, but I do not have enough information on your project to know what constraints you have.

UPDATE: Nice little npm package https://www.npmjs.com/package/live-sql seems to solve your problem I hope

sparrow
  • 1,888
  • 11
  • 22
  • The database rows are being inserted elsewhere, not in the NodeJS app. – Chud37 Aug 31 '17 at 07:40
  • Heres' another one that doesn't require setting up a replication slave, depending on if thats possible: https://www.npmjs.com/package/mysql-events – sparrow Aug 31 '17 at 07:56
  • 1
    Thank you so much, I got it working in the end. The documentation in the live-sql package is a little dodgy, but with some trial and error it works! – Chud37 Sep 02 '17 at 17:24