Database Listener (Node.js + Socket.io) - mysql

I'm trying to learn Node.js and Socket.io
I'm trying to create a page that will display the values of a database table whenever changes are detected.
For simplicity, I would only be changing the values of the database table via PHPMyAdmin manually.
Would this be possible? If so, could you point me in the right direction so that I can try it out.

Related

Flutter MySql1 Realtime data

I can't make data run in Realtime which mean data that I want display always update if have change in database. right now I just can make data display if I push button to request data from MySQL workbench. I using MySQL1
Please help me almost a month I try to solve this problem T_T

MySQL "No database selected" (Using API)

I'm in the middle of making a React API Spring-boot App with a SQL database (named persons). Everything is launching alright and the database is showing on the react app. But! I made this database days ago and when I go back into Terminal to find it using SELECT * from persons; it is telling me
No database selected".
I also put in 'Describe persons;' ...and nothing. Once you make a database and make a API out of it, can you not change the name of the tables and the content? How can I access this again?

Perform an action when a new MySQL row is created using Node

I am making a Slack bot in node. I have a website with a MySQL database, and whenever someone makes a new post, a new row is added to the database. I want to send a message in Slack when a new post is created, but I can't figure out how to trigger some code when a new line is added.
I am using Node, the MySQL plugin, and the Slack API, but that last part shouldn't matter.
I just need a code snippet that can trigger some other code whenever there is a new row in a specific table.
Thanks in advance!
The easiest solution is to track events by tailing MYSQL BIN logs
https://www.npmjs.com/package/mysql-events
This npm package can track insert, update, and delete events.
Alternatively, you can setup MYSQL triggers for various events(INSERT in your case).
But there is a limitation for the MYSQL triggers, you cannot trigger NodeJs code but it can only run SQL stored procedure or queries.
You can create one postEventLog table and setup after insert trigger on it for each inserted row.
https://www.mysqltutorial.org/mysql-triggers/mysql-after-insert-trigger/
Then you can keep checking for new changes in the table at a specified interval.
In case, you don't want an extra table. Keep polling for new changes in the post table, if found new records then perform your custom logic for calling slack API/ notify the client.

How to render a graph when Mysql data is updated in Vue?

I organized like this,
Back-end: Nodejs - Insert a data to MYSQL periodically.
DB: MYSQL
Front-end: Vue.js - Draw and Re-render a Graph with Mysql Data when data is updated.
In this situation, do I have to check a MYSQL data per seconds whether it is latest or not? and then I have to render a graph again?
Or is there a way like when MYSQL data is updated, triggering a specific methods that is re-render a graph(automatically).
I prefer a second way though, I have no idea it is possible.
Someone can give an advises me plz? or example?
What you could do is using socket.io for this. The moment you update your database in Node.js you then also send a notification or the data over a socket, which tells your Vue.js application that it needs to load new data or gives it the data directly.
I used the following npm packages for my setup with Vue.js and Node.js:
Vue: socket.io-client
Node: socket.io
I followed these 2 sites when I faced a similar problem to get a hang of sockets:
https://socket.io/get-started/chat/#Introduction
https://medium.com/#jaouad_45834/basic-chat-web-app-using-express-js-vue-js-socket-io-429588e841f0

Django database watchdog save signal outside django

I have the following problem:
I Am using a Django framework.
One of the parts in a system (non-django) writes to the database, in the same database that django is using.
I want to have a signal when an object is being saved. It's a django model object but not saved via django, but directly in the mysql database.
Is there a way django can watch save-actions in his database when it's not being saved by django?
The neatest way would be: create an Api, and let the save action run through this api. The save signal can than be django default. (but this depends on some work of externals... so not the prefered route... for future development it sure is).
Another option is to implement celery and create a task that frequently looks whether one of the saved objects has had no follow up..... (also quit some puzzling I guess to get this up and running)
But there might be an easier... for me unknown?
I saw django watchdog solutions for file systems... not for databases (probably because django has this build in... when properly done through django)
to complex it: I test and develop locally with sqlite .... but the save signal I can put in my tests without needing to get this locally working.... as long as it works in mysql, I Am happy.
You can try this solution:
Create a new table 'django_watch' with one column 'object_id' (add other columns like 'created_datetime' etc according to your standards);
Lets say your main table is 'object'. Add a mysql trigger for the INSERT event on this table.
You should add an extra insert query inside the trigger to insert the object_id into 'django_watch' table.
Now you can have a cronjob that will be inpecting the new table 'django_watch' (for updations in Django objects) and perform necessary actions. You can run this cronjob continuously with some 1 minute delay (upto you).
In the end, I wrote an api that can be called by the thirdparty module. I delivered the code to logon on django using c-code to this api and call the GET of this api. (using django rest framework). This api just saves the object (the id given in the url), and from there on it's default django. The only thing the third party had to do is build in my code to call the api as well....
Maybe not the best solution, but the best to implement for my problem....