基于 Socket.IO 的 PHP 应用程序 + 基于 WebSocket 的通知 [英] PHP application + WebSocket based notifications over Socket.IO

查看:34
本文介绍了基于 Socket.IO 的 PHP 应用程序 + 基于 WebSocket 的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Laravel 用 PHP 编写的 Web 应用程序.

I have a web application written in PHP using laravel.

现在,我必须添加实时通知系统,从服务器 -> 客户端推送消息,并且可以检索从客户端 -> 服务器推送的消息.

Now, I have to add realtime notification system that pushes messages from server -> client, and can retrieve messages pushed from client -> server.

由于 Socket.IO 向后兼容并且可以优雅地降级,我想将其用于 WebSocket/AJAX 轮询部分.

Since Socket.IO is backwards compatible and degrades gracefully, I want to use it for the WebSocket / AJAX polling part.

问题是 Socket.IO 默认使用 Node.JS 后端.

The problem is that Socket.IO by default works with Node.JS backend.

我拥有服务器的 root 访问权限,并行运行(Apache 和 Node)或添加任何额外内容都没有问题.

I have root access to the server, there is no problem running both (Apache and Node) in parallel or adding anything extra.

问题是,如何从 PHP 向 Node + Sockets.IO 发送消息?

然后,如何将消息从 Sockets.IO + Node 传输到 PHP?

我已经将 Node + Sockets 留给浏览器部分了,因为这很简单.

I have left the Node + Sockets to Browser part out, because that's pretty straight forward.

我见过使用 Express.js + CURL 和/或 POST 请求的示例,但是,难道没有更底层的方法吗?

I have seen examples utilising Express.js + CURL and/or POST requests, but, isn't there a lower level way of doing it?

我知道 DNode + PHP,但我无法想象如何组装所有东西 - Apache、PHP、Node、Socket.IO、DNode,以便它们无缝地协同工作.

I am aware of DNode + PHP, but I cannot imagine how to assemble everything - Apache, PHP, Node, Socket.IO, DNode so it would work together seamlessly.

此外,我已将此 PHP WebSockets 服务器 实现转换为 Laravel 库.但是,这个库的问题在于它在客户端使用了干净的 WebSockets.而且,我找不到将它与 Socket.IO 客户端 一起破解的方法.

Also, I have transformed into a laravel library this PHP WebSockets server implementation. The problem with this library, though, is that it utilizes clean WebSockets for it's client side. And, I couldn't find a way to hack it together with Socket.IO client.

推荐答案

本教程 使用 PHP 和 Socket.IO 回答您问题的主要部分.

This tutorial on using PHP and Socket.IO answers the main part of your question.

socket.io 不是您唯一的选择.查看实时网络技术指南.从表面上看,目前最好的基于 PHP 的解决方案是 Ratchet.这需要作为单独的进程运行,因此您仍然会遇到跨进程通信问题.

socket.io isn't your only options. Have a look at the realtime web tech guide. By the looks of it the best PHP-based solution right now is Ratchet. This needs to run as a separate process so you're still going to have the cross-process communication problem.

常见的跨进程通信问题是使用消息队列.当您希望在 Apache 上运行的 PHP 应用程序从实时 Web 技术中获知事件时,问题就出现了.您可能不得不通过 Web 调用来执行此操作,即使它是从实时 Web 技术进程到 apache 服务器的调用.

The common cross process communication problem is to use messages queues. The problem then comes in when you want your PHP app running on Apache to be informed of events from the realtime web technology. You're probably going to have to do that via a web call, even if it's a call from the realtime web tech process to the apache server.

另一种选择是将实时通信卸载到托管服务,例如我为之工作的 Pusher.

Another option is to offload the realtime communication to a hosted service like Pusher who I work for.

最近的这个问题也很相关,有一些有用的链接:对 Web 套接字的原生 PHP 支持是否可用?

This recent question is also relevant and has some useful links: Is native PHP support for Web Sockets available?

这篇关于基于 Socket.IO 的 PHP 应用程序 + 基于 WebSocket 的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆