Nginx Push Stream Module 介绍

用途

nginx的Push Stream Module使用http技术来实现连接管道,在项目里主要用于即时消息的推送,比如聊天功能。

Push Stream Module主要采用pub/sub模式来管理长连接,用户可以申请连接通道,通道建立订阅该通道,消息推送者可以向连接通道发送消息,这样订阅该通道的所有用户都可以接收到该消息。

安装

方法1:

# clone the project
git clone http://github.com/wandenberg/nginx-push-stream-module.git
NGINX_PUSH_STREAM_MODULE_PATH=$PWD/nginx-push-stream-module
cd nginx-push-stream-module

# build with 1.0.x, 0.9.x, 0.8.x series
./build.sh master 1.0.5
cd build/nginx-1.0.5

# install and finish
sudo make install

# check
sudo /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.0.5

# test configuration
sudo /usr/local/nginx/sbin/nginx -c $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf -t
the configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf syntax is ok
configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf test is successful

# run
sudo /usr/local/nginx/sbin/nginx -c $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf
方法2:

# clone the project
git clone http://github.com/wandenberg/nginx-push-stream-module.git
NGINX_PUSH_STREAM_MODULE_PATH=$PWD/nginx-push-stream-module

# get desired nginx version (works with 1.0.x, 0.9.x, 0.8.x series)
wget http://nginx.org/download/nginx-1.0.5.tar.gz

# unpack, configure and build
tar xzvf nginx-1.0.5.tar.gz
cd nginx-1.0.5
./configure --add-module=../nginx-push-stream-module
make

# install and finish
sudo make install

# check
sudo /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.0.5

# test configuration
sudo /usr/local/nginx/sbin/nginx -c $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf -t
the configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf syntax is ok
configuration file $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf test is successful

# run
sudo /usr/local/nginx/sbin/nginx -c $NGINX_PUSH_STREAM_MODULE_PATH/misc/nginx.conf
基本配置

1. 订阅通道,服务器收到订阅请求,如果通道不存在则会建立通道,配置如下:

location ~ /sub/(.*) {
        # activate subscriber (streaming) mode for this location
        push_stream_subscriber;

        # positional channel path
        set $push_stream_channels_path              $1;
}

push_stream_subscriber指令用于指定该location用来订阅通道;$push_stream_channels_path参数指定要订阅的通道。

2. 向指定的通道发送发送消息,配置如下:

location /pub {
        # activate publisher (admin) mode for this location
        push_stream_publisher admin;

        # query string based channel id
        set $push_stream_channel_id             $arg_id;
}
3. 查看通道状态,通过该设置可以查看服务器上的所有或者指定的通道数,通道的订阅数,发送过的消息数量等等,基本配置如下:

location /stats {
        # activate channels statistics mode for this location
        push_stream_channels_statistics;

        # query string based channel id
        set $push_stream_channel_id             $arg_id;
}
基本用法

配置好并重启nginx之后就可以开始使用Push Stream Module了。

在session A中订阅通道:

curl http://comet.com/sub/my_channel_1

可以看见此时还未有任何返回信息;

在session B中向该通道发送消息:

curl -d hello http://comet.com/pub?id=my_channel_1
收到json格式的返回数据:{"channel": "my_channel_1", "published_messages": "1", "stored_messages": "0", "subscribers": "1"};

切回session A可以看见接收到的消息hello;

在session B中查看通道状态:

curl http://comet.com/stats;
curl http://comet.com/stats?id=my_channel_1;
curl http://comet.com/stats?id=my_channel_*;
接收到统计数据:{"hostname": "local", "time": "2012-09-08T11:09:52", "channels": "0", "broadcast_channels": "0", "published_messages": "2", "subscribers": "0", "uptime": "867", "by_worker": [
{"pid": "12072", "subscribers": "0", "uptime": "719"}]};

在session B中删除通道:

curl -X DELETE 'http://comet.com/pub?id=my_channel_1'

参考文章:http://wiki.nginx.org/HttpPushStreamModule


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值