Suleman Ahmad
Suleman Ahmad

Reputation: 2113

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

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?

Upvotes: 3

Views: 7621

Answers (2)

WatsMyName
WatsMyName

Reputation: 4478

well in your scenario you need a listener, which tracks the moment the new row of chat is inserted to the database.

It will be easier if you emit chat message to the users before you insert it into the database.

You can use socket. you can either use nodejs+socket.io to perform this. Here is bit about that

socket.io and node.js to send message to particular client

You can also look this article

https://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0

Upvotes: 2

Vyktor
Vyktor

Reputation: 21007

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.

Upvotes: 4

Related Questions