Socketelx.Config (socketelx v0.4.0)

Copy Markdown View Source

Per-instance configuration.

Resolves explicit options first (passed to Socketelx.start_link/1), falling back to environment variables for anything omitted — the same variables the standalone release reads. This is what lets the exact same code run either as a hex dependency embedded in your own supervision tree (config passed explicitly) or as a standalone release configured purely by env vars, with zero code differences.

Options / environment variables

OptionEnv varDefault
:app_keySOCKETELX_APP_KEY"app-key"
:app_secretSOCKETELX_APP_SECRET"app-secret" (public; a warning is logged, always set your own)
:app_idSOCKETELX_APP_ID"app-id"
:portPORT6001
:webhook_urlSOCKETELX_WEBHOOK_URLnone
:client_messages_enabledSOCKETELX_ENABLE_CLIENT_MESSAGEStrue
:redis_urlSOCKETELX_REDIS_URLnone (single node)
:redis_opts—[] (extra Redix options, e.g. ssl: true)
:clusterSOCKETELX_CLUSTERnone (set to erlang for the experimental Redis-free cluster mode)
:cluster_nodesSOCKETELX_CLUSTER_NODESnone (comma-separated Erlang node names to connect to, e.g. socketelx@host1,socketelx@host2)
:cluster_dnsSOCKETELX_CLUSTER_DNSnone (a hostname resolving to every peer; nodes are named <basename>@<ip>)
:cluster_node_basenameSOCKETELX_CLUSTER_NODE_BASENAME"socketelx" (the part before @ when using :cluster_dns)
:webhook_retriesSOCKETELX_WEBHOOK_RETRIES3 (retries after the first attempt; 0 disables)
:webhook_retry_base_ms—1000 (delay doubles on each retry)
:rest_rate_limitSOCKETELX_REST_RATE_LIMITnone (max REST API requests per second, per instance)
:client_event_rate_limitSOCKETELX_CLIENT_EVENT_RATE_LIMITnone (max client events per second, per connection)
:max_connectionsSOCKETELX_MAX_CONNECTIONSnone (max WebSocket connections on this node)
:batch_limitSOCKETELX_BATCH_LIMIT10 (max events per batch_events request)
:metricsSOCKETELX_METRICSfalse (serve Prometheus metrics at GET /metrics)
:metrics_tokenSOCKETELX_METRICS_TOKENnone (if set, /metrics requires Authorization: Bearer <token>)
:allowed_originsSOCKETELX_ALLOWED_ORIGINSnone (every origin may connect; see below)
:max_connections_per_ipSOCKETELX_MAX_CONNECTIONS_PER_IPnone (max simultaneous WebSocket connections from one client IP)
:client_ip_headerSOCKETELX_CLIENT_IP_HEADERnone (read the client IP from this header, e.g. cf-connecting-ip; see below)
:webhook_queue_pathSOCKETELX_WEBHOOK_QUEUE_PATHnone (a directory; failed webhooks are kept there and survive a restart)
:tls_certfileSOCKETELX_TLS_CERTFILEnone (with :tls_keyfile, serve HTTPS/WSS directly)
:tls_keyfileSOCKETELX_TLS_KEYFILEnone
:appsSOCKETELX_APPSnone (several apps in one server, see Socketelx and the multi-app guide)

Allowed origins

Browsers send an Origin header on every WebSocket connection. With :allowed_origins unset (or containing "*") every origin is accepted. Otherwise the origin must match one entry: a full origin ("https://chat.example.com", scheme and port included), a bare host ("chat.example.com", any scheme or port) or a wildcard host ("*.example.com", any subdomain but not example.com itself). Give a list, or a comma-separated string in the environment variable. A refused connection receives pusher:error 4009 and is closed. Connections without an Origin header (servers, mobile apps) are not affected: the check protects against other websites using your visitors' browsers, it is not authentication.

Setting :redis_url switches the instance to the Redis backend so several nodes can share channels, presence and broadcasts. Setting cluster: :erlang does the same over Erlang's own clustering with no Redis (experimental, see Socketelx.Backend.Distributed). Without either, the instance runs fully in-memory and needs nothing but this library. Setting both is an error.

Summary

Functions

Whether a browser Origin header value may open a WebSocket on this instance. nil (no header) is always allowed; see the module docs.

Types

t()

@type t() :: %Socketelx.Config{
  allowed_origins: term(),
  app_id: term(),
  app_key: term(),
  app_secret: term(),
  backend: term(),
  batch_limit: term(),
  client_event_rate_limit: term(),
  client_ip_header: term(),
  client_messages_enabled: term(),
  cluster: term(),
  cluster_dns: term(),
  cluster_node_basename: term(),
  cluster_nodes: term(),
  max_connections: term(),
  max_connections_per_ip: term(),
  metrics: term(),
  metrics_token: term(),
  node_id: term(),
  port: term(),
  prefix: term(),
  redis_opts: term(),
  redis_url: term(),
  rest_rate_limit: term(),
  tls_certfile: term(),
  tls_keyfile: term(),
  webhook_queue_path: term(),
  webhook_retries: term(),
  webhook_retry_base_ms: term(),
  webhook_url: term()
}

Functions

origin_allowed?(name, origin)

Whether a browser Origin header value may open a WebSocket on this instance. nil (no header) is always allowed; see the module docs.