基于 Socket.IO 的 PHP 应用程序 + 基于 WebSocket 的通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12216787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:03:35  来源:igfitidea点击:

PHP application + WebSocket based notifications over Socket.IO

phpapachenode.jswebsocketsocket.io

提问by jolt

I have a web application written in PHP using laravel.

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

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

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

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

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

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

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

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

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

The problem is, how do I send a message from PHP to Node + Sockets.IO?

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

And then, how do I transfer a message from Sockets.IO + Node to PHP?

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

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

我已经将 Node + Sockets to Browser 部分排除在外,因为这非常简单。

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

我已经看到使用Express.js + CURL 和/或 POST requests 的示例,但是,没有更低级别的方法吗?

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.

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

Also, I have transformed into a laravel library this PHP WebSockets serverimplementation. 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 WebSockets 服务器实现转换为 Laravel 库。但是,这个库的问题在于它在客户端使用了干净的 WebSockets。而且,我找不到将它与Socket.IO client一起破解的方法。

采纳答案by leggetter

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

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

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.

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

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.

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

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

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

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

最近的这个问题也很相关,并且有一些有用的链接:是否有本机 PHP 支持 Web 套接字?

回答by Placeholder

Elephant.IOsupports PHPwith Socket.IOso take a look at it!

Elephant.IO通过Socket.IO支持PHP,所以看看它!

回答by Thomas Clayson

There isn't an easy way to communicate between node and apache/php. So you're either going to have to write all of your functionality again in node (connecting to mysql databases and such) or create an API to call from Node and pass to sockets.

在 node 和 apache/php 之间没有一种简单的通信方式。因此,您要么必须在 node 中再次编写所有功能(连接到 mysql 数据库等),要么创建一个 API 以从 Node 调用并传递给套接字。

However you could use a jquery plugin like Jquery graceful websocketsand this php classto get your own websockets functionality with jquery and php, rather than using sockets.io.

但是,您可以使用 jquery 插件,如Jquery 优雅的 websockets这个 php 类,通过 jquery 和 php 获得您自己的 websockets 功能,而不是使用 sockets.io。