用途
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": [
在session B中删除通道:
curl -X DELETE 'http://comet.com/pub?id=my_channel_1'
参考文章:http://wiki.nginx.org/HttpPushStreamModule