将Node.js与Symfony2或PHP集成 [英] Integrate Node.js with Symfony2 or PHP

查看:85
本文介绍了将Node.js与Symfony2或PHP集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2开发一个Web应用程序。我需要创建一个推送通知sysmte(如Facebook)。当用户发布内容时,我需要其他一些用户收到通知。



我看到Node.js这是最简单的方法。但是,我做了一些简单的例子,一切正常,但我不知道如何将这个node.js应用程序与我的Symfony2应用程序集成,或者真的与PHP应用程序集成。



有人可以帮帮我吗?



提前致谢!

解决方案

请注意,您没有提供足够的详细信息,因此我将作为前端开发人员而不是移动开发者回复



集成NodeJS和PHP(一般情况下)并不是一个好方法,因为你需要分别启动两个服务器,在你的应用程序使用PHP时用JS创建websocket服务器,最后从PHP创建一个请求(GET或POST)你JS服务器。好吧,这是一个很大的混乱,所以我会在这里公开我的解决方案。



快速洞察移动应用程序。嗯,从技术上讲,没有简单的方法。您可以使用Push协议( http://www.wikiwand.com/en/Push_technology)使用NotificationPusher( https://github.com/Ph3nol/NotificationPusher )。我以前没用过它所以我无法帮助你。



一般来说。
大部分时间当人们想到Push时,长时间的民意调查会起到作用。对于初学者来说,这意味着请求是客户端的,服务器不发送数据和关闭连接,直到有新数据。



你如何实现这个?!
基本上,你改变 max_execution_time 使用 ini_set set_time_limit 在当前脚本的很长时间内启动循环(如do..while),并进行睡眠和检查你的数据在里面。从您的Javascript中只需进行Ajax调用,例如使用jquery:$ .get。请记住删除超时并保持异步模式。



此解决方案的唯一缺点是您将始终与服务器建立连接,这将消耗一些更多的移动设备上的电池。如果您有多种类型的数据需要接收,请不要犹豫,合并调用并在响应数据中发布类型,因为大多数浏览器只允许同时连接2个或3个同一服务器。 / p>

I'm developing a web application with Symfony2. I need to create a push notifications sysmte (like Facebook). When an user publish something, I need some of another users receive a notification.

I saw that Node.js it's the easiest manner to do this. But, I did some simple examples and all works fine, but I don't know how can I integrate this node.js application with my Symfony2 application, or really with a PHP application.

Anybody can help me?

Thanks in advance!

解决方案

Please note that you've not given enough details, so I will respond as a front-end developer and not as a mobile developer

Integrating NodeJS and PHP (in general) is not a good way since you need to launch both servers separatly, create the websocket server in JS while your application is in PHP and finally create a request (GET or POST) from your PHP to you JS server. Well, a big mess, so I'll expose my solution here under.

Quick insight for mobile apps. Well, technically, there's no easy way. You can use the Push "protocol" (http://www.wikiwand.com/en/Push_technology) with NotificationPusher (https://github.com/Ph3nol/NotificationPusher). I didn't used it before so I can't help you with it.

In general. Most of the time when people thinks of Push, long polling will do the trick. For starters it means that the request is made client-side and the server don't send data & close connection until there's new data.

How do you implement this ?!? Basically, you change the max_execution_time using ini_set or set_time_limit to a very long time for the current script and launch a loop (like a do..while) with a sleep and the check to your data inside. From your Javascript just make an Ajax call, for example with jquery: $.get. Just remember to remove the timeout and stay in asynchronous mode.

The only drawback of this solution is that you will always have a connection opened to your server which will consume a bit more of battery on a mobile device. If you have multiple types of data to receive do not hesitate to merge the calls and publish a type in your response data, since most of the browsers allows only 2 or 3 simultaneous connections to the same server.

这篇关于将Node.js与Symfony2或PHP集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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