7

I have a PHP app built and running on Apache, using Nginx as a reverse proxy to serve static resources.

I also have Redis installed which I am using to store activity ID's for each users activity stream. The activity gets written to a MySQL database, then Redis pushes the activity ID into each users stream. When a user receives his/her activity stream, the app first retrieves the users list of activity ID's from Redis and then gets the actual activity data via a MySQL IN() query.

This all works very well, however I want to start add real time capability to this set up. I would like to be able to push these events straight to a users browser and also add general live notifications.

For this I have installed node.js with socket.io. I have the socket.io server correctly up and running and a client is automatically connected on page load.

Where I am struggling is in understanding how to post a message to socket.io from within my PHP app. Since PHP and node.js cannot communicate directly, my understanding is that I would be best off utilizing Redis as a go-between since I already have it installed and up and running. However I have no idea how to go about this.

What I need is a description of the process (any code examples would be very helpful) of sending a notification from PHP to Redis and then into socket.io in order for it to be pushed to the relevant client.

Also, I do not understand how socket.io would know which client to send to. How would I pass this information along and keep everything synced? Is this even necessary? Do I need to store my PHP sessions in Redis and have socket.io collect the data when a user connects? Or is there another way?

Thanks in advance.

Please Note: My PHP SESSION data is currently saved to disk.

3
  • how about having a pubsub channel on redis, and subscribing to it from your node.js process. Your PHP would then publish to pubsub to communicate to the node. you can have separate pubsub channels for different clients and publish to whatever channels you need to reach your specific clients. Commented May 10, 2013 at 18:01
  • @akonsu This is exactly what I ended up doing thanks. However I set up only a single pubsub channel and publish everything to that just to get it into socket.io. There I work out which specific client it should go to etc... Would I be better off defining a separate pubsub channel for each connected client? Also if you want to write this up i more detail as an answer I will mark it correct. :) Commented May 11, 2013 at 5:28
  • Honestly, I do not know about creating a separate channel for each user. It depends on the number of users. It might not scale. But I would definitely keep in mind the possibility of having several channels. One day when your user base grows, you could have a number of related redis instances and shard your channels to them. Commented May 13, 2013 at 4:30

2 Answers 2

1

You can set up a pubsub channel on Redis (please see http://redis.io/topics/pubsub). Then subscribe to it from your node.js process. Your PHP would then publish to pubsub to communicate to the node. You can have separate pubsub channels for different clients and publish to whatever channels you need to reach your specific clients.

Sign up to request clarification or add additional context in comments.

Comments

0

Redis doesn't offer inbuilt websocket or http server so we have to integrate it with php or node.js in order to stream channel data. with Tweak method, we can connect Redis server with php using predis php library for redis, where php will push data into Redis and socket.io will keep track of new messages pushed into Redis sever and beam it back to users connected to it in real time.

https://github.com/u-day/tweak/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.