I want to send a response to all users which are currently active (Logged or viewing content) when the database data changes.
Example
Some user is viewing “data” table.
Currently I’m executing AJAX calls every 5 seconds and if there is change in data database response the client will be notified.
However I no longer want to do it this way.
The alternative for ajax polling (as you did) is using comet/server-side push.
In other words, you would use something like WebSockets, Ajax long-polling, Server-Sent Events, etc., to send messages to the client instead of the client polling.
For the sake of this example, I’m going to use WebSockets. What I describe below applies to the other methods as well, though.
If you already have an existing PHP application, the simplest way to do this would probably be to write a separate “daemon” script which handles the sockets. Whenever your application changes some data in the DB, you would send a message to the daemon script, which would then notify the connected clients of the change.
You could use something like ZeroMQ for messaging between the main app and the socket daemon.