简体   繁体   English

socket.io node.js与简单的node.js ajax与php / ajax(用于数据)

[英]socket.io node.js vs simple node.js ajax vs php/ajax (for data)

Hello StackOverFlow community, 您好StackOverFlow社区,

My Configuration : 我的配置:

I'm building a php app based on an MVC (C module) and I am using nginx. 我正在基于MVC(C模块)构建一个php应用程序,并且正在使用nginx。 The session is stored in Redis (session server). 会话存储在Redis(会话服务器)中。

And I have node.js and socket.io runing to manage all the realTime things (Chat, presence on the page, etc...) 而且我正在运行node.js和socket.io来管理所有实时事件(聊天,页面上的状态等等)。

The node.js and php uses the same session from redis (the authentification is done only once in the PHP side) and node.js uses cookies module to use it. node.js和php使用来自redis的相同会话(身份验证仅在PHP端执行一次),而node.js使用cookie模块使用它。

The socket.io is on a port and in nginx configuration I root /socket.io/ on that port.(streaming way). socket.io在端口上,并且在nginx配置中,我在该端口上/root.socket.io /。(流方式)。

The issue or question : 问题或问题:

The thing is that Speed is very important and I don't like php for that (the code has to compile everytime), and I need it realTime '!'. 问题是速度非常重要,我不喜欢php(每次都要编译代码),而我需要realTime'!'。 so For now I ask my socket.io to tell my visitor to do an ajax call on the php (triger the call from client side). 所以现在我要求我的socket.io告诉我的访问者在php上进行ajax调用(从客户端触发该调用)。 but I don't like it not clean. 但我不喜欢它不干净。

The server is 256GB RAM, 8 cores/16 threads, process 2.8-3.5GHz and the maximum visitor I'll have on each of those categories of server at the same time will not go over 1000 visitor. 该服务器为256GB RAM,8核/ 16线程,进程为2.8-3.5GHz,同时我在所有这些服务器类别上拥有的最大访问者不会超过1000个访问者。 with different timezone so maximum 10 to 60 req/seconds. 使用不同的时区,因此最大为10到60请求/秒。

Can I use socket.io for my data and calls ? 我可以使用socket.io进行数据和调用吗? I mean I don't have to use the event as "submit" then "on" I can do a submit() and make a callback so I don't have to send headers etc each time. 我的意思是,我不必将事件用作“提交”,而不必在“ on”上使用,我可以执行submit()并进行回调,因此我不必每次都发送标头等。

And also the alternative would be to use ReactPHP (compile once then keep runing). 另外一种选择是使用ReactPHP(编译一次然后继续运行)。

Do you think it'll be stable ? 您认为它会稳定吗? (97.5%) is enough. (97.5%)就足够了。 do you have any suggestion ? 你有什么建议吗? Please feel free to correct me also :). 也请随时纠正我:)。 If I am doing something wrong. 如果我做错了。

It sounds like you have a good grasp on the logistics and have been able to build a solution that works but that you're a bit dissatisfied with its overall architecture. 听起来您对物流有很好的了解,并且能够构建一个可行的解决方案,但是您对它的总体体系结构有些不满意。

One approach that many PHP projects use when incorporating real-time features is to use an external realtime network to pass data between clients and the server as peers. 许多PHP项目在合并实时功能时使用的一种方法是使用外部实时网络在客户端和服务器之间以对等方式传递数据。 For example, PubNub provides a JavaScript SDK that allows you to publish and subscribe to chat events on different channels. 例如,PubNub提供了一个JavaScript SDK ,使您可以在不同渠道上发布和订阅聊天事件。 On your client, you could use this code: 在您的客户端上,您可以使用以下代码:

<script src=http://cdn.pubnub.com/pubnub.min.js ></script>
<script>(function(){

  var pubnub = PUBNUB.init({
    publish_key   : 'demo',
    subscribe_key : 'demo'
  })

  pubnub.publish({
    channel : "chat",
    message : "Hi."
  })

})();</script>

One benefit of this model in your case would be that the clients would not be forced to make server calls, or wait on the server's response, in order to proceed. 在您的情况下,此模型的好处是不会强迫客户端继续进行服务器调用或等待服务器的响应。 Speaking of the server, here's how you would subscribe to your chat channel (assuming you are using 5.3): 说到服务器,这是您如何订阅chat频道的方法(假设您使用的是5.3):

$pubnub = new Pubnub(
  "demo",  ## PUBLISH_KEY
  "demo",  ## SUBSCRIBE_KEY
  "",      ## SECRET_KEY
  false    ## SSL_ON?
);

$pubnub->subscribe(array(
  'channel'  => 'chat',        ## REQUIRED Channel to Listen
  'callback' => function($message) {  ## REQUIRED Callback With Response
    var_dump($message);  ## Print Message
    return true;         ## Keep listening (return false to stop)
  }
));

PubNub also has an HTML5 chat example using Presence ( non-technical overview ), which allows you to determine users' connectivity status. PubNub还有一个使用PresenceHTML5聊天示例非技术概述 ),它使您可以确定用户的连接状态。

By moving all this activity away from your server and onto a globally-distributed realtime network, you can simplify your architecture, while maintaining your PHP server. 通过将所有这些活动从服务器移到全球分布的实时网络上,可以简化架构,同时维护PHP服务器。

Good luck! 祝好运!

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

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