Redix.PubSub.start_link
start_link
, go back to Redix.PubSub module for more information.
Specs
start_link(String.t() | keyword()) :: :gen_statem.start_ret()
Starts a pub/sub connection to Redis.
This function returns {:ok, pid}
if the PubSub process is started successfully.
The actual TCP/SSL connection to the Redis server may happen either synchronously,
before start_link/2
returns, or asynchronously: this behaviour is decided by
the :sync_connect
option (see below).
This function accepts one argument, either a Redis URI as a string or a list of options.
Redis URI
In case uri_or_opts
is a Redis URI, it must be in the form:
redis://[:password@]host[:port][/db]
Here are some examples of valid URIs:
redis://localhost
redis://:secret@localhost:6397
redis://example.com:6380/1
Usernames before the password are ignored, so the these two URIs are equivalent:
redis://:secret@localhost
redis://myuser:secret@localhost
The only mandatory thing when using URIs is the host. All other elements (password, port, database) are optional and their default value can be found in the "Options" section below.
Options
The following options can be used to specify the parameters used to connect to Redis (instead of a URI as described above):
:host
- (string) the host where the Redis server is running. Defaults to"localhost"
.:port
- (integer) the port on which the Redis server is running. Defaults to6379
.:password
- (string) the password used to connect to Redis. Defaults tonil
, meaning no password is used. When this option is provided, all Redix does is issue anAUTH
command to Redis in order to authenticate. This can be used to fetch the password dynamically on every reconnection but most importantly to hide the password from crash reports in case the Redix connection crashes for any reason. For example, you can usepassword: {System, :fetch_env!, ["REDIX_PASSWORD"]}
.:database
- (integer or string) the database to connect to. Defaults tonil
, meaning don't connect to any database (Redis connects to database0
by default). When this option is provided, all Redix does is issue aSELECT
command to Redis in order to select the given database.:socket_opts
- (list of options) this option specifies a list of options that are passed to:gen_tcp.connect/4
when connecting to the Redis server. Some socket options (like:active
or:binary
) will be overridden byRedix.PubSub
so that it functions properly. Defaults to[]
.:sync_connect
- (boolean) decides whether Redix should initiate the TCP connection to the Redis server before or after returning fromstart_link/2
. This option also changes some reconnection semantics; read the "Reconnections" page in the docs forRedix
for more information.:backoff_initial
- (integer) the initial backoff time (in milliseconds), which is the time that will be waited by theRedix.PubSub
process before attempting to reconnect to Redis after a disconnection or failed first connection. See the "Reconnections" page in the docs forRedix
for more information.:backoff_max
- (integer) the maximum length (in milliseconds) of the time interval used between reconnection attempts. See the "Reconnections" page in the docs forRedix
for more information.:exit_on_disconnection
- (boolean) iftrue
, the Redix server will exit if it fails to connect or disconnects from Redis. Note that setting this option totrue
means that the:backoff_initial
and:backoff_max
options will be ignored. Defaults tofalse
.:name
- Redix is bound to the same registration rules as aGenServer
. See theGenServer
documentation for more information.:ssl
- (boolean) iftrue
, connect through SSL, otherwise through TCP. The:socket_opts
option applies to both SSL and TCP, so it can be used for things like certificates. See:ssl.connect/4
. Defaults tofalse
.:sentinel
- (list of options) exactly the same as the:sentinel
options inRedix.start_link/1
.:hibernate_after
- (integer) if present, the Redix connection process awaits any message for the given number of milliseconds and if no message is received, the process goes into hibernation automatically (by calling:proc_lib.hibernate/3
). See:gen_statem.start_opt/0
. Not present by default.:spawn_opt
- (options) if present, its value is passed as options to the Redix connection process as inProcess.spawn/4
. See:gen_statem.start_opt/0
. Not present by default.:debug
- (options) if present, the corresponding function in the:sys
module is invoked. Not present by default.
Examples
iex> Redix.PubSub.start_link()
{:ok, #PID<...>}
iex> Redix.PubSub.start_link(host: "example.com", port: 9999, password: "secret")
{:ok, #PID<...>}
iex> Redix.PubSub.start_link([database: 3], [name: :redix_3])
{:ok, #PID<...>}
Specs
start_link(String.t(), keyword()) :: :gen_statem.start_ret()
Same as start_link/1
but using both a Redis URI and a list of options.
In this case, options specified in opts
have precedence over values specified by uri
.
For example, if uri
is redix://example1.com
but opts
is [host: "example2.com"]
, then
example2.com
will be used as the host when connecting.