跳转至

push-stream: NGINX 推送流模块

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7、8、9 和 10
  • CentOS 7、8、9
  • AlmaLinux 8、9
  • Rocky Linux 8、9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-push-stream
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-push-stream

通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用模块:

load_module modules/ngx_http_push_stream_module.so;

本文档描述了 nginx-module-push-stream v0.6.0,于 2024 年 5 月 9 日发布。


为您的 Nginx 设置提供纯粹的流式 HTTP 推送技术。

Comet 变得简单且 真正可扩展

支持 EventSourceWebSocket、长轮询和 Forever Iframe。请参见下面的 一些示例

_此模块不与 Nginx 源代码一起分发。请参见 安装说明

在 GitHub 上可用,地址为 nginx_push_stream_module

更新日志

请始终查看 CHANGELOG.textile 以了解最新内容。

贡献

在您尝试并喜欢此模块后,欢迎您 回馈一些东西,并帮助维护该项目 ;)

基本配置

    # 将 push_stream_shared_memory_size 添加到您的 http 上下文
    http {
       push_stream_shared_memory_size 32M;

        # 在您的服务器上下文中定义发布者和订阅者端点
        server {
           location /channels-stats {
                # 为此位置激活频道统计模式
                push_stream_channels_statistics;

                # 基于查询字符串的频道 ID
                push_stream_channels_path               $arg_id;
            }

            location /pub {
               # 为此位置激活发布者(管理员)模式
               push_stream_publisher admin;

                # 基于查询字符串的频道 ID
                push_stream_channels_path               $arg_id;
            }

            location ~ /sub/(.*) {
                # 为此位置激活订阅者(流式)模式
                push_stream_subscriber;

                # 位置参数频道路径
                push_stream_channels_path                   $1;
            }
        }
    }

基本用法

您现在可以在命令行中体验一下。尝试使用多个终端并开始玩 HTTP pubsub:

    # 订阅
    curl -s -v --no-buffer 'http://localhost/sub/my_channel_1'
    curl -s -v --no-buffer 'http://localhost/sub/your_channel_1'
    curl -s -v --no-buffer 'http://localhost/sub/your_channel_2'

    # 发布
    curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'
    curl -s -v -X POST 'http://localhost/pub?id=your_channel_1' -d 'Hi everybody!'
    curl -s -v -X POST 'http://localhost/pub?id=your_channel_2' -d 'Goodbye!'

    # 发布者的频道统计(json 格式)
    curl -s -v 'http://localhost/pub?id=my_channel_1'

    # 所有频道统计汇总(json 格式)
    curl -s -v 'http://localhost/channels-stats'

    # 所有频道统计详细信息(json 格式)
    curl -s -v 'http://localhost/channels-stats?id=ALL'

    # 带前缀的频道统计详细信息(json 格式)
    curl -s -v 'http://localhost/channels-stats?id=your_channel_*'

    # 频道统计(json 格式)
    curl -s -v 'http://localhost/channels-stats?id=my_channel_1'

    # 删除频道
    curl -s -v -X DELETE 'http://localhost/pub?id=my_channel_1'

一些示例  

常见问题  

有疑问吗?!请查看 常见问题解答

错误报告  

报告错误时,请在适用时提供以下信息:

  1. 使用的推送流模块版本(commit sha1)?
  2. 使用的 nginx 版本?
  3. 正在使用的 Nginx 配置
  4. “nginx -V” 命令输出
  5. 指示模块代码失败的核心转储。请查看 这里 了解如何生成。
  6. 逐步描述以重现错误。

谁在使用该模块?  

您使用此模块吗?请在 列表 上留下您的名字。

Javascript 客户端  

有一个 JavaScript 客户端实现 在这里,它与框架无关。尝试并帮助改进它。 ;)

指令

(1) 定义位置,(2) 主要配置,(3) 订阅者配置,(4) 发布者配置,(5) 频道统计配置,(6) WebSocket 配置

指令 (1) (2) (3) (4) (5) (6)
push_stream_channels_statistics   x   -   -   -   -   -
push_stream_publisher   x   -   -   -   -   -
push_stream_subscriber   x   -   -   -   -   -
push_stream_shared_memory_size   -   x   -   -   -   -
push_stream_channel_deleted_message_text   -   x   -   -   -   -
push_stream_channel_inactivity_time   -   x   -   -   -   -
push_stream_ping_message_text   -   x   -   -   -   -
push_stream_timeout_with_body   -   x   -   -   -   -
push_stream_message_ttl   -   x   -   -   -   -
push_stream_max_subscribers_per_channel   -   x   -   -   -   -
push_stream_max_messages_stored_per_channel   -   x   -   -   -   -
push_stream_max_channel_id_length   -   x   -   -   -   -
push_stream_max_number_of_channels   -   x   -   -   -   -
push_stream_max_number_of_wildcard_channels   -   x   -   -   -   -
push_stream_wildcard_channel_prefix   -   x   -   -   -   -
push_stream_events_channel_id   -   x   -   -   -   -
push_stream_channels_path   -   -   x   x   x   x
push_stream_store_messages   -   -   -   x   -   x
push_stream_channel_info_on_publish   -   -   -   x   -   -
push_stream_authorized_channels_only   -   -   x   -   -   x
push_stream_header_template_file   -   -   x   -   -   x
push_stream_header_template   -   -   x   -   -   x
push_stream_message_template   -   -   x   -   -   x
push_stream_footer_template   -   -   x   -   -   x
push_stream_wildcard_channel_max_qtd   -   -   x   -   -   x
push_stream_ping_message_interval   -   -   x   -   -   x
push_stream_subscriber_connection_ttl   -   -   x   -   -   x
push_stream_longpolling_connection_ttl   -   -   x   -   -   -
push_stream_websocket_allow_publish   -   -   -   -   -   x
push_stream_last_received_message_time   -   -   x   -   -   -
push_stream_last_received_message_tag   -   -   x   -   -   -
push_stream_last_event_id   -   -   x   -   -   -
push_stream_user_agent   -   -   x   -   -   -
push_stream_padding_by_user_agent   -   -   x   -   -   -
push_stream_allowed_origins   -   -   x   -   -   -
push_stream_allow_connections_to_events_channel   -   -   x   -   -   x

内存使用

以下是每个对象所使用的最小内存量的信息:

  • 共享内存中的消息 = 200 字节
  • 共享内存中的频道 = 270 字节
  • 订阅者 共享内存中的 = 160 字节 系统中的 = 6550 字节

测试

此模块的服务器测试是用 Ruby 编写的,并且是验收测试,点击 这里 了解更多细节。

讨论

Nginx 推送流模块 讨论组

贡献者

人员

GitHub

您可以在 nginx-module-push-stream 的 GitHub 仓库 中找到此模块的其他配置提示和文档。