Sock

Build StatusDocsHex.pm

Sock is a specification for apps to service WebSocket connections; you can think of it as 'Plug for WebSockets'. Web servers such as Bandit or Cowboy are expected to implement support for hosting Sock-based applications (possibly by way of an adapter library such as Plug.Cowboy). WebSocket-aware applications such as Phoenix can then be hosted within a supported web server simply by defining conformance to the Sock behaviour, in the same manner as how Plug conformance allows their HTTP aspects to be hosted within an arbitrary web server.

The Sock specification is just that; a specification. There is no actual code in this specification as there is within the Plug specification, largely due to the lower-level nature of WebSockets as compared to HTTP. What you will find here consists of simple interface which dictates conventions about how the lifecycle of a WebSocket connection is managed.

The Sock behaviour describes the functions that an application must implement in order to be Sock compliant; it is the equivalent of the Plug interface, but for WebSocket connections. Server implementations are expected to manage the upgrade process to WebSocket connections themselves, based on their specific policy and routing decisions and aided by helper functions within the Plug API (see below for details).

WebSocket Lifecycle

WebSocket connections go through a well defined lifecycle, which is reflected in the shape of the Sock behaviour:

For more information, consult the docs.

Installation

The sock package can be installed by adding sock to your list of dependencies in mix.exs:

def deps do
  [
    {:sock, "~> 0.3.1"}
  ]
end

Documentation can be found at https://hexdocs.pm/sock.

License

MIT