Long Polling Options: Nginx, PHP, Node.js

安稳与你 提交于 2019-12-03 06:49:13

I would use option C: and would suggest an option D:

option D:

  • Keepalived with HAProxy for load balancing (LB)
  • Nginx for static and PHP scripts, using PHP-FPM, APC and Redis for caching
  • Node.js (and other Node modules) for dynamic, realtime content

We currently use the first 2 parts of option D, coming from a LAMP background, and are currently implementing Node.js to serve some of our (system taxing) realtime apps. HAProxy does exactly that: proxies the traffic to all my backend servers, instead of having Nginx doing it. Reason for that, we have many backend HTTP/TCP/other servers and we require redundant and automatic failover to these servers. LB is simple to implement and works well.

So far, excellent results. Personally, the Nodes learning curve has so far has been difficult due to lack of documentation, but there is a very dynamic community out there.

Hope this helps.

I, personally would use only Node.js. Instead of a long poll, you can push new information to all available clients. Node.JS is extremely fast when delivering realtime content and is capable of doing everything in one package. Also, the client side and server side is written in javascript making it easier to develop, debug, and deliver. As a developer you can see the benefits of this.

Here is an example of an app using Node.js and the modules express, jade and NowJS. Of course this can also be used as a combination with your CMS running on apache and Node.JS serving the dynamic content. with either Nginx or a node script acting as a reverse proxy in front of this script and Apache.

A simple chat application

Server.js

var express = require('express')
  , app = express.createServer()
  , nowjs = require('now')

/* configure express server */
//...

app.get('/', function(req, res){
  res.render('chat')
})

var everyone = nowjs.initialize(app)

// Server scoped function called by single client
everyone.now.distributeMsg = function(msg){
  // Client scoped function of every connected client
  this.now.receiveMsg(msg)
}

app.listen(3000)

chat.jade

!!!
html
  head
    script(type='text/javascript', src='/nowjs/now.js')
  body
    #log
    input#entry(type='text')
    input#submit(type='button')

script
  $(function(){
    $('#submit').click(function(){
      now.distributeMsg($('#entry').val())
    })

    now.receiveMsg = function(msg){
      $('#log').append('<div>' + msg + '</div>')
    }
  })

Yes, it REALLY is that simple and wouldn't take many more lines of code to turn this into a full featured chat application. In fact your markup and CSS would take more lines than the application code. Amazing!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!