Need help setting up comet code

Someone knows the way, or you might think that you can connect Node.js using the Nginx push-push module to maintain a constant connection between the client and the browser.

I am new to comets, so I just do not understand publications, etc., maybe someone can help me.

What I have installed so far is as follows. I downloaded the jQuery.comet plugin and installed the following base code:

Client javascript

<script type="text/javascript">

    function updateFeed(data) {
        $('#time').text(data);
    }

    function catchAll(data, type) {
        console.log(data);
        console.log(type);
    }

    $.comet.connect('/broadcast/sub?channel=getIt');
    $.comet.bind(updateFeed, 'feed');
    $.comet.bind(catchAll);

    $('#kill-button').click(function() {
        $.comet.unbind(updateFeed, 'feed');
    });
</script>

What I can understand from this is that the client will continue to listen to the URL followed by / broadcast / sub = getIt. When a message appears, it will launch updateFeed.

Pretty simple and straightforward IMO.

Nginx http HTTP module configuration

default_type application / octet-stream; sendfile on; keepalive_timeout 65; push_authorized_channels_only off;

server {
  listen       80;
  location /broadcast {
    location = /broadcast/sub {
      set $push_channel_id $arg_channel;
      push_subscriber;
      push_subscriber_concurrency broadcast;
      push_channel_group broadcast;
    }

    location = /broadcast/pub {
      set $push_channel_id $arg_channel;
      push_publisher;
      push_min_message_buffer_length 5;
      push_max_message_buffer_length 20;
      push_message_timeout 5s;
      push_channel_group broadcast;
    }
  }
}

nginx, 80 /broadcast/sub, , /broadcast/pub.

. . php , .

- Node.js /broadcast/pub. , persistent streaming data , . , , .

.

Node.js

Node.js . , , Node.js .

, , :

var sys = require('sys'), 
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(new Date());
    res.close();
    seTimeout('',1000);
}).listen(8000);

8000 .

nginx.config :

server {
  listen      80;
  server_name _;

  location / {
    proxy_pass   http://mydomain.com:8080$request_uri;
    include      /etc/nginx/proxy.conf;
  }
 }

80 8000, .

- , Node.js , . , .

Recources

faye, , , Nginx. . nginx .

+3
1

, , pub/sub Nginx, Ruby . :

script , Nginx, , , , , . , Nginx , , .

Nginx ( BTW, ).

: , . , WebSockets .

Node.js, Nginx ( ).

+2

Source: https://habr.com/ru/post/1739695/


All Articles