Configure Centrifugo
Let's look at how Centrifugo can be configured.
This chapter describes configuration principles and some important configuration options. There are some options not mentioned here but described later in each feature documentation.
Configuration sources
Centrifugo can be configured in several ways.
Command-line flags
Centrifugo supports several command-line flags. See centrifugo -h for available flags. Command-line flags limited to most frequently used. In general, we suggest to avoid using flags for configuring Centrifugo in a production environment – prefer environment or configuration file sources.
Command-line options have the highest priority when set than other ways to configure Centrifugo.
OS environment variables
All Centrifugo options can be set over env in the format CENTRIFUGO_<OPTION_NAME> (i.e. option name with CENTRIFUGO_ prefix, all in uppercase).
Setting options over env is mostly straightforward except namespaces – see how to set namespaces via env. Environment variables have the second priority after flags.
Boolean options can be set using strings according to Go language ParseBool function. I.e. to set true you can just use "true" value for an environment variable (or simply "1"). To set false use "false" or "0". Example:
export CENTRIFUGO_PROMETHEUS="1"
Also, array options, like allowed_origins can be set over environment variables as a single string where values separated by a space. For example:
export CENTRIFUGO_ALLOWED_ORIGINS="https://mysite1.example.com https://mysite2.example.com"
For a nested object configuration (which we have, for example, in Centrifugo PRO ClickHouse analytics) it's still possible to use environment variables to set options. In this case replace nesting with _ when constructing environment variable name.
Empty environment variables are considered unset (!) and will fall back to the next configuration source.
Configuration file
Configuration file supports all options mentioned in Centrifugo documentation and can be in one of three supported formats: JSON, YAML, or TOML. Config file options have the lowest priority among configuration sources (i.e. option set over environment variable prevails over the same option in config file).
A simple way to start with Centrifugo is to run:
centrifugo genconfig
This command generates config.json configuration file in a current directory. This file already has the minimal number of options set. So it's then possible to start Centrifugo:
centrifugo -c config.json
Config file formats
Centrifugo supports three configuration file formats: JSON, YAML, or TOML.
JSON config format
Here is an example of Centrifugo JSON configuration file:
{
"allowed_origins": ["http://localhost:3000"],
"token_hmac_secret_key": "<YOUR-SECRET-STRING-HERE>",
"api_key": "<YOUR-API-KEY-HERE>"
}
token_hmac_secret_key used to check JWT signature (more info about JWT in authentication chapter). If you are using connect proxy then you may use Centrifugo without JWT.
api_key used for Centrifugo API endpoint authorization, see more in chapter about server HTTP API. Keep both values secret and never reveal them to clients.
allowed_origins option described below.
TOML config format
Centrifugo also supports TOML format for configuration file:
centrifugo --config=config.toml
Where config.toml contains:
allowed_origins: [ "http://localhost:3000" ]
token_hmac_secret_key = "<YOUR-SECRET-STRING-HERE>"
api_key = "<YOUR-API-KEY-HERE>"
log_level = "debug"
In the example above we also defined logging level to be debug which is useful to have while developing an application. In the production environment debug logging can be too chatty.
YAML config format
YAML format is also supported:
allowed_origins:
- "http://localhost:3000"
token_hmac_secret_key: "<YOUR-SECRET-STRING-HERE>"
api_key: "<YOUR-API-KEY-HERE>"
log_level: debug
With YAML remember to use spaces, not tabs when writing a configuration file.
Important options
Let's describe some important options you can configure when running Centrifugo.
allowed_origins
This option allows setting an array of allowed origin patterns (array of strings) for WebSocket and SockJS endpoints to prevent CSRF or WebSocket hijacking attacks. Also, it's used for HTTP-based unidirectional transports to enable CORS for configured origins.
As soon as allowed_origins is defined every connection request with Origin set will be checked against each pattern in an array.
Connection requests without Origin header set are passing through without any checks (i.e. always allowed).
For example, a client connects to Centrifugo from a web browser application on http://localhost:3000. In this case, allowed_origins should be configured in this way:
"allowed_origins": [
"http://localhost:3000"
]
When connecting from https://example.com:
"allowed_origins": [
"https://example.com"
]
Origin pattern can contain wildcard symbol * to match subdomains:
"allowed_origins": [
"https://*.example.com"
]
– in this case requests with Origin header like https://foo.example.com or https://bar.example.com will pass the check.
It's also possible to allow all origins in the following way (but this is discouraged and insecure when using connect proxy feature):
"allowed_origins": [
"*"
]
address
Bind your Centrifugo to a specific interface address (string, by default "" - listen on all available interfaces).
port
Port to bind Centrifugo to (string, by default "8000").
engine
Engine to use - memory, redis or tarantool. It's a string option, by default memory. Read more about engines in special chapter.
Advanced options
These options allow tweaking server behavior, in most cases default values are good to start with.
client_channel_limit
Default: 128
Sets the maximum number of different channel subscriptions a single client can have.
When designing an application avoid subscribing to an unlimited number of channels per one client. Keep number of subscriptions for each client reasonably small – this will help keeping handshake process lightweight and fast.
channel_max_length
Default: 255
Sets the maximum length of the channel name.
client_user_connection_limit
Default: 0
The maximum number of connections from a user (with known user ID) to Centrifugo node. By default, unlimited.
The important thing to emphasize is that client_user_connection_limit works only per one Centrifugo node and exists mostly to protect Centrifugo from many connections from a single user – but not for business logic limitations. This means that if you set this to 1 and scale nodes – say run 10 Centrifugo nodes – then a user will be able to create 10 connections (one to each node).
client_queue_max_size
Default: 1048576
Maximum client message queue size in bytes to close slow reader connections. By default - 1mb.
client_anonymous
Default: false
Enable a mode when all clients can connect to Centrifugo without JWT. In this case, all connections without a token will be treated as anonymous (i.e. with empty user ID) and only can subscribe to channels with anonymous option enabled.
client_concurrency
Default: 0
client_concurrency when set tells Centrifugo that commands from a client must be processed concurrently.
By default, concurrency disabled – Centrifugo processes commands received from a client one by one. This means that if a client issues two RPC requests to a server then Centrifugo will process the first one, then the second one. If the first RPC call is slow then the client will wait for the second RPC response much longer than it could (even if the second RPC is very fast). If you set client_concurrency to some value greater than 1 then commands will be processed concurrently (in parallel) in separate goroutines (with maximum concurrency level capped by client_concurrency value). Thus, this option can effectively reduce the latency of individual requests. Since separate goroutines are involved in processing this mode adds some performance and memory overhead – though it should be pretty negligible in most cases. This option applies to all commands from a client (including subscribe, publish, presence, etc).
gomaxprocs
Default: 0
By default, Centrifugo runs on all available CPU cores. To limit the number of cores Centrifugo can utilize in one moment use this option.
Endpoint configuration.
After Centrifugo started there are several endpoints available.