简体   繁体   中英

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

Hello StackOverFlow community,

My Configuration :

I'm building a php app based on an MVC (C module) and I am using nginx. The session is stored in Redis (session server).

And I have node.js and socket.io runing to manage all the realTime things (Chat, presence on the page, etc...)

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.

The socket.io is on a port and in nginx configuration I root /socket.io/ on that port.(streaming way).

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 '!'. 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). 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. with different timezone so maximum 10 to 60 req/seconds.

Can I use socket.io for my data and calls ? 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.

And also the alternative would be to use ReactPHP (compile once then keep runing).

Do you think it'll be stable ? (97.5%) is enough. 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. For example, PubNub provides a JavaScript SDK that allows you to publish and subscribe to chat events on different channels. 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):

$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.

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.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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