1

I have been working around a bit with event-listeners in JavaScript as of late and also started working on a small browser-based irc-module and it got me thinking. Is it possible to add an event-listener to the php-script that would update variables, arrays or even echo out the data directly from a database?

I have read PHP Event-Listener best-practice implementation among others, but haven't really understood exactly how that would help me. Perhaps I'm just slow at this point, but it would be a great boost if someone could explain to me how to set up an event-listener that would listen towards a mysql-database for changes.

I do not ask anyone to code for me, but to tell me if it works and perhaps even explain how. I do not want to have a javascript-intervall checking the php-file every 100 milisecond to keep the chat flowing, that would only slow it down in the end.

If I have explained myself badly, do say so and I shall try better. I do not have the best reputation for explanations.

Community
  • 1
  • 1
Richard Atterfalk
  • 462
  • 1
  • 10
  • 23
  • "listen towards a mysql-database for changes" Have you thought about triggers? – Marco Aurélio Deleu Jun 13 '12 at 18:18
  • I still have a lot to learn it seems. I shall check up on triggers, but could you give me an example on how that could look like? – Richard Atterfalk Jun 13 '12 at 18:20
  • An experimental approach: if all data is routed through a persistent PHP process, then it could notify clients via websockets. But PHP is not really a good fit for that type of application. – Matthew Jun 13 '12 at 18:23
  • I could check up on the websockets then! Seems like a very valid idea... But if not PHP, then what would you recommend? I'm always up to learn something new. – Richard Atterfalk Jun 13 '12 at 18:24
  • First note that Websockets are not supported by all browsers, so it's not a end-all solution. If you are just trying out, you could use PHP. I've not got any real experience working with them yet, but I wouldn't be surprised if people have made good implementations with node.js (server side tech). – Matthew Jun 13 '12 at 18:33
  • I almost forgot about node.js! I have been looking for an opportunity to use it, and now when I had I forgot about it. Thanks you. – Richard Atterfalk Jun 13 '12 at 18:38

1 Answers1

0

There is no way in PHP to listen to events from MySQL. The MySQL libraries do not send events. MySQL will not send info to PHP except when PHP code requests it.

You could write a script to poll for changes if you can figure out how to recognize them. Or to read the MySQL query log and react to that. But there's nothing in MySQL that will kick off any PHP code.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • Strange, I have to say. Talked to a person today that I know got a rather deep knowledge in this subject that told me that there is a way to check this. I might have missunderstood him and are now looking at this from the wrong angle. – Richard Atterfalk Jun 13 '12 at 18:21