push_stream_message_template

The `push_stream_message_template` directive sets a template for the message format in the NGINX Push Stream module.

Syntaxpush_stream_message_template string;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The push_stream_message_template directive is used to define a template string for the messages that are published to channels using the NGINX Push Stream module. When this directive is specified, it allows the publisher of the message to format the output by including dynamic content through variable substitution. This is particularly useful for creating structured or customized message formats that can include information such as timestamps, channel names, or user IDs.

To use this directive, you simply provide a string as the argument, which may include specific placeholders that will be replaced with values at runtime. Common placeholders include variables such as $time_local for the current time, and $channel for the channel name. The output format can be adjusted easily by modifying the template string, enabling flexible message structures.

By setting this directive in the appropriate context (http, server, location), you ensure that messages sent to subscribers of a specific channel adhere to your predefined template. This flexibility in presentation can be vital for applications requiring precise interactions between publishers and subscribers with varied data needs.

Config Example

http {
    push_stream_message_template "[{\"time\": \"$time_local\", \"channel\": \"$channel\", \"message\": \"$message\"}]";
    server {
        location /pub {
            push_stream_publisher;
            push_stream_channels_path $arg_id;
        }
        location ~ /sub/(.*) {
            push_stream_subscriber;
            push_stream_channels_path $1;
        }
    }
}

Ensure that the placeholders used in the template string exist in the context of the push stream message.

Quotes within the template string need to be escaped properly to avoid syntax errors in the configuration file.

← Back to all directives