使用Redis在PHP和socket.io/node.js之间进行通信

时间:2022-08-22 14:36:58

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

我在Apache上构建并运行了一个PHP应用程序,使用Nginx作为反向代理来提供静态资源。

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.

我还安装了Redis,我用它来存储每个用户活动流的活动ID。该活动将写入MySQL数据库,然后Redis将活动ID推送到每个用户流中。当用户收到他/她的活动流时,应用程序首先从Redis检索活动ID的用户列表,然后通过MySQL IN()查询获取实际活动数据。

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.

为此,我已经使用socket.io安装了node.js.我正确启动并运行了socket.io服务器,并在页面加载时自动连接客户端。

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.

我正在努力的方法是了解如何从我的PHP应用程序中向socket.io发布消息。由于PHP和node.js无法直接通信,我的理解是我最好将Redis用作中间人,因为我已经安装并启动并运行它。但是我不知道该怎么做。

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.

我需要的是从PHP发送通知到Redis然后再发送到socket.io以便将其推送到相关客户端的过程描述(任何代码示例都非常有用)。

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?

另外,我不明白socket.io如何知道要发送到哪个客户端。我如何传递这些信息并保持一致?这甚至是必要的吗?我是否需要将我的PHP会话存储在Redis中并让socket.io在用户连接时收集数据?或者还有另一种方式吗?

Thanks in advance.

提前致谢。

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

请注意:我的PHP SESSION数据当前已保存到磁盘。

2 个解决方案

#1


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.

您可以在Redis上设置pubsub频道(请参阅http://redis.io/topics/pubsub)。然后从node.js进程订阅它。然后,您的PHP将发布到pubsub以与节点进行通信。您可以为不同的客户提供单独的pubsub渠道,并发布到您需要的任何渠道以联系您的特定客户。

#2


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.

Redis不提供内置的websocket或http服务器,所以我们必须将它与php或node.js集成,以便传输通道数据。使用Tweak方法,我们可以使用用于redis的predis php库将Redis服务器与php连接,其中php将数据推送到Redis,socket.io将跟踪推送到Redis服务器的新消息并将其发送回与实际连接的用户时间。

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

#1


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.

您可以在Redis上设置pubsub频道(请参阅http://redis.io/topics/pubsub)。然后从node.js进程订阅它。然后,您的PHP将发布到pubsub以与节点进行通信。您可以为不同的客户提供单独的pubsub渠道,并发布到您需要的任何渠道以联系您的特定客户。

#2


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.

Redis不提供内置的websocket或http服务器,所以我们必须将它与php或node.js集成,以便传输通道数据。使用Tweak方法,我们可以使用用于redis的predis php库将Redis服务器与php连接,其中php将数据推送到Redis,socket.io将跟踪推送到Redis服务器的新消息并将其发送回与实际连接的用户时间。

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