admin 管理员组

文章数量: 1086019

I'm slowly digging in and learning redis in my spare time, and I am interested in the options available for creating a 'listener' for a website that subscribes to a channel, and updates a webpage as messages are received.

Now, from my old actionscript days, and current javascript work, i'm quite familiar with the concept of listeners given those two languages. However, my server-side-programming-fu really only extends as far as PHP, a bit of rails, a bit of python, and pseudo node.js (i'm a fairly experienced javascript guy, and understand what node.js does).

Since my main skill set lies in php, i'm wondering if it is even possible to do a persistent connection/socket with php and thus create a listener in PHP?

Since i'm guessing this isn't actually possible (or is the equivalent of fixing a windshield crack with a hammer), what are some server side options? Is it possible to just create a javascript listener that uses a persistent connection to the redis server (currently on localhost). Is Socket.io something I should look into?

Any insight for a redis beginner would be much 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 aside from node.js that does the same thing? I'm ok with dropping php pletely and trying something new for this project. It's a personal one anyways.

I'm slowly digging in and learning redis in my spare time, and I am interested in the options available for creating a 'listener' for a website that subscribes to a channel, and updates a webpage as messages are received.

Now, from my old actionscript days, and current javascript work, i'm quite familiar with the concept of listeners given those two languages. However, my server-side-programming-fu really only extends as far as PHP, a bit of rails, a bit of python, and pseudo node.js (i'm a fairly experienced javascript guy, and understand what node.js does).

Since my main skill set lies in php, i'm wondering if it is even possible to do a persistent connection/socket with php and thus create a listener in PHP?

Since i'm guessing this isn't actually possible (or is the equivalent of fixing a windshield crack with a hammer), what are some server side options? Is it possible to just create a javascript listener that uses a persistent connection to the redis server (currently on localhost). Is Socket.io something I should look into?

Any insight for a redis beginner would be much 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 aside from node.js that does the same thing? I'm ok with dropping php pletely and trying something new for this project. It's a personal one anyways.

Share Improve this question edited May 23, 2017 at 10:32 CommunityBot 11 silver badge asked Dec 9, 2011 at 18:15 Jonathan CoeJonathan Coe 1,4854 gold badges19 silver badges36 bronze badges 3
  • I dont quite get what you want to acplish. Since PHP is server side, the page will be updated upon page load. If you are still looking for an event/observer type of mechanism you can use this one: ponents.symfony-project/event-dispatcher. And this might also be interesting for you rediska.geometria-lab/documentation/usage/publish-subscribe. But again, I dont quite get, what you want to do.. – Kristian Hildebrandt Commented Dec 9, 2011 at 20:40
  • @KristianHildebrandt I'm more or less exploring live update type applications... however, after doing a lot of digging, the only real way to do this is to build it out using something like node.js, which is built to use listeners anyways. Regardless, thanks for the links! – Jonathan Coe Commented Dec 14, 2011 at 22:53
  • @Jonathan Coe: Please accept an answer. – McK Commented Jan 18, 2016 at 12:06
Add a ment  | 

3 Answers 3

Reset to default 2

The pub/sub mechanisms within redis require a subscribed client to be persistent, that is "always on" in order to receive the updates via the subscription.

It is possible to daemon-ize a PHP script/application, but it is not ideal, nor is it one of PHP's core petencies.

I would remend looking to another solution. If you found the node.js + redis + pub/sub post you mentioned pelling, perhaps you should consider it more strongly.

You could also consider re-thinking how you architect your redis-stored data. Perhaps a set, from which items can be "popped" would suit the same purpose without requiring the use of redis pub/sub.

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

If all you are looking to do is subscribe to a channel in redis and then do some work server side then it seems like PHP will work just fine. A cursory glance at Predis and phpredis show they both allow subscribing to a redis channel and registering a callback which fires whenever a message is received.

本文标签: javascriptCreating a redis listenerpossible in phpStack Overflow