Reason
Reason

Reputation: 59

Pushing data to client whenever a database field changes

I'm using socket.io to send data to client from my database. But my code send data to client every second even the data is the same. How can I send data only when field in db was changed not every 1 second.

Here is my code: http://pastebin.com/kiTNHgnu

Upvotes: 0

Views: 1152

Answers (1)

mscdex
mscdex

Reputation: 106696

With MySQL there is no easy/simple way to get notified of changes. A couple of options include:

  • If you have access to the server the database is running on, you could stream MySQL's binlog through some sort of parser and then check for events that modify the desired table/column. There are already such binlog parsers on npm if you want to go this route.

  • Use a MySQL trigger to call out to a UDF (user-defined function). This is a little tricky because there aren't a whole lot of these for your particular need. There are some that could be useful however, such as mysql2redis which pushes to a Redis queue which would work if you already have Redis installed somewhere. There is a STOMP UDF for various queue implementations that support that wire format. There are also other UDFs such as log_error which writes to a file and sys_exec which executes arbitrary commands on the server (obviously dangerous so it should be an absolute last resort). If none of these work for you, you may have to write your own UDF which does take quite some time (speaking from experience) if you're not already familiar with the UDF C interface.

    I should also note that UDFs could introduce delays in triggered queries depending on how long the UDF takes to execute.

Upvotes: 1

Related Questions