简体   繁体   English

Socket.io和Ajax请求到php页面

[英]Socket.io and ajax request to php page

I'm setting up a realtime app that will be using socket.io. 我正在设置一个将使用socket.io的实时应用程序。 There's currently some core functionally in php, that utilizes memcache and mysql backend. 当前在php中有一些功能上的核心,它利用了memcache和mysql后端。

Would it make sense in the socket.io server to do an ajax request (if that's even possible) to the php page that handles this? 在socket.io服务器中向处理此问题的php页面发出ajax请求(如果可能的话)是否有意义? There's a lot of MySQL querying, I know it can be done in node.js, but I'd rather keep this part abstracted in php if possible. 有很多MySQL查询,我知道可以在node.js中完成,但如果可能的话,我宁愿将此部分保留在php中。

So, my question is, is that a proper thing to do? 所以,我的问题是,这是正确的做法吗? Call a php page from within the socket.io server to then return to the client? 从socket.io服务器中调用php页面,然后返回到客户端?

Thanks! 谢谢!

I don't see any problems with having your node.js app communicate with your PHP app by exposing a RESTful API or some PHP script that you can POST to or GET from your socket.io node.js server. 通过暴露RESTful API或一些PHP脚本(您可以将它们发布到socket.io node.js服务器上或从其中获取),使您的node.js应用程序与PHP应用程序通信不会出现任何问题。 There are plenty of npm modules (like request ) that can make HTTP requests like that a breeze for you. 有很多npm模块(例如request )可以使HTTP请求变得轻而易举。 After retrieving the data from PHP in your node app, you can use socket.io to emit() the data to the socket.io client on the frontend. 在节点应用程序中从PHP检索数据后,可以使用socket.io将数据emit()到前端的socket.io客户端。

There is nothing wrong with that. 没有什么不妥。 You are simply using a RESTful API to access the MySQL data, thus isolating the database details. 您只需使用RESTful API来访问MySQL数据,从而隔离数据库详细信息。

If one day you are tired of PHP, you can easily switch to Ruby, Python or Whatever for that part without even touching the node.js. 如果有一天您对PHP感到厌倦,则可以轻松切换到Ruby,Python或Whatever,而无需接触node.js。 If your logic is already written in PHP (you are upgrading an old app), it make even more sense as you can reuse what has already been tested and debugged. 如果您的逻辑已经用PHP编写(您正在升级旧的应用程序),那么当您可以重用已经测试和调试的内容时,它的意义就更大了。 A lot of folks are advocating for that kind of separation between systems. 许多人提倡在系统之间进行这种分离。 Just look at all the SOA (Service Oriented Architecture) buzz. 只需看一下所有SOA(面向服务的体系结构)的嗡嗡声。

Where I work we are using this very architecture in a project (though in this case its an ASP.NET MVC Website calling a Java EE app) and it served us very well. 在我工作的地方,我们在项目中使用了这种架构(尽管在本例中是一个称为Java EE应用程序的ASP.NET MVC网站),它为我们提供了很好的服务。 With the event model of node.js, its even better since you won't block waiting for the PHP. 使用node.js的事件模型,它会更好,因为您不会阻塞等待PHP。

But of course, there are some drawback 但是当然有一些缺点

  • Performance overhead 性能开销
  • Architecture is more complicated 建筑更复杂
  • You now work with two language instead of only one (though javascript and PHP are so often used together that I don't think it's really is a problem in this case) 您现在可以使用两种语言,而不是仅使用一种语言(尽管javascript和PHP经常一起使用,以至于我认为在这种情况下这确实不是问题)

So you need to ask yourself if your problem really need that solution. 因此,您需要问自己,您的问题是否确实需要该解决方案。 But in a lot of case the answer may be yes. 但在很多情况下,答案可能是肯定的。 Just don't forget the virtue of Keeping It Simple and Stupid (the KISS principle) 只是不要忘记保持简单和愚蠢的优点(KISS原则)

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

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