Creating a redis listener - maybe in php?

I slowly dig around and study redis in my free time, and I'm interested in the options available to create a “listener” for a site that subscribes to the channel, and refreshes the web page as it receives messages.

Now, from my old days of actioncript and the current work of javascript, I am well acquainted with the concept of listeners, given these two languages. However, my server-programming-fu really only extends to PHP, a little rails, a bit of python and a pseudo node.js (I am a pretty experienced javascript guy and understand that node.js).

Since my main skill set is in php, I wonder if it is even possible to make a permanent connection / socket with php and thereby create a listener in PHP?

Since I assume that this is actually not possible (or is it the equivalent of fixing a windshield crack with a hammer), what are some server-side options? Is it possible to simply create a javascript listener that uses a persistent connection to the redis server (currently on the local host). Is Socket.io something I should look at?

Any insight for a beginner redis would be greatly appreciated.


edit I found a great post here How to use redis PUBLISH / SUBSCRIBE with nodejs to notify clients when data values ​​change? that partially answers my question.

Is there a method other than node.js that does the same? I am fine with completely dropping php and trying something new for this project. It is personal anyway.

+4
source share
3 answers

The pub / auxiliary mechanisms in redis require that the signed client be permanent, that is, "always on" in order to receive updates through a subscription.

You can daemon-ize a PHP script / application, but it is not ideal and is not one of the core competencies of PHP.

I would recommend looking for another solution. If you find node.js + redis + pub / sub post that you called convincing, maybe you should think about it more.

You can also think about how you architect your data stored in redis. Perhaps the set from which the elements can be “slipped” will do the same without requiring the use of redis pub / sub.

+2
source

If all you want to do is subscribe to the channel in redis and then make part of the production server, then it seems like PHP will work fine. A quick look at Predis and phpredis shows that they both enable subscription to the redis channel and register a callback that fires whenever a message is received.

+1
source

PubSubContext and DispatcherLoop examples from the Predis source code on Github are different implementations of what you are trying to achieve in PHP.

+1
source

Source: https://habr.com/ru/post/1385581/


All Articles