简体   繁体   English

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

[英]PHP application + WebSocket based notifications over Socket.IO

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 server implementation.此外,我已将此PHP WebSockets 服务器实现转换为 Laravel 库。 The problem with this library, though, is that it utilizes clean WebSockets for it's client side.但是,这个库的问题在于它在客户端使用了干净的 WebSockets。 And, I couldn't find a way to hack it together with Socket.IO client .而且,我找不到将它与Socket.IO client一起破解的方法。

This tutorial on using PHP and Socket.IO answers the main part of your question.本教程使用 PHP 和 Socket.IO回答了您问题的主要部分。

socket.io isn't your only options. socket.io 不是您唯一的选择。 Have a look at the realtime web tech guide .查看实时网络技术指南 By the looks of it the best PHP-based solution right now is Ratchet .从表面上看,目前最好的基于 PHP 的解决方案是Ratchet This needs to run as a separate process so you're still going to have the cross-process communication problem.这需要作为单独的进程运行,因此您仍然会遇到跨进程通信问题。

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.当您希望在 Apache 上运行的 PHP 应用程序从实时 Web 技术中获知事件时,问题就出现了。 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.您可能不得不通过 Web 调用来执行此操作,即使它是从实时 Web 技术进程到 apache 服务器的调用。

Another option is to offload the realtime communication to a hosted service like Pusher who 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 套接字?

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

There isn't an easy way to communicate between node and apache/php.在 node 和 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 中再次编写所有功能(连接到 mysql 数据库等),要么创建一个 API 以从 Node 调用并传递给套接字。

However you could use a jquery plugin like Jquery graceful websockets and this php class to get your own websockets functionality with jquery and php, rather than using sockets.io.但是,您可以使用 jquery 插件,如Jquery 优雅的 websockets这个 php 类,通过 jquery 和 php 获得您自己的 websockets 功能,而不是使用 sockets.io。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM