Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Listener in PHP/Mysql, whenever any record inserted into table?

Tags:

php

mysql

Is their any method/way that we come to know, any change occurs in desired table?

I have implemented one solution that it checks in db every 30 sec, that is if any change occur in chats table then refresh my chats listing. it waists lot of performance and slow down the site.

Is their any way that our action listener only calls whenever any row is inserted in table?

Any idea?

like image 333
Suleman Ahmad Avatar asked Jan 19 '26 12:01

Suleman Ahmad


1 Answers

You may create table with notifications (which will always contains just unread messages for last 2 hours or so) and then create trigger [syntax] that will create new notification each time change occurs:

CREATE TRIGGER create_notification AFTER INSERT ON chats
FOR EACH ROW INSERT INTO notifications (...) VALUES( NEW.col1, NEW.col2)...

Then you'll end up with much smaller (and faster) database table.

There are some alternatives like writing a socket server but I think it would be less resource effective than pooling database plus I have a bad experience running php scripts for a long time.

like image 150
Vyktor Avatar answered Jan 21 '26 04:01

Vyktor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!