| 1 |
= cowboy_websocket(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_websocket - Websocket |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The module `cowboy_websocket` implements Websocket |
| 10 |
as a Ranch protocol. It also defines a callback interface |
| 11 |
for handling Websocket connections. |
| 12 |
|
| 13 |
== Callbacks |
| 14 |
|
| 15 |
Websocket handlers must implement the following callback |
| 16 |
interface: |
| 17 |
|
| 18 |
[source,erlang] |
| 19 |
---- |
| 20 |
init(Req, State) |
| 21 |
-> {cowboy_websocket, Req, State} |
| 22 |
| {cowboy_websocket, Req, State, Opts} |
| 23 |
|
| 24 |
websocket_init(State) -> CallResult %% optional |
| 25 |
websocket_handle(InFrame, State) -> CallResult |
| 26 |
websocket_info(Info, State) -> CallResult |
| 27 |
|
| 28 |
terminate(Reason, PartialReq, State) -> ok %% optional |
| 29 |
|
| 30 |
Req :: cowboy_req:req() |
| 31 |
PartialReq :: map() |
| 32 |
State :: any() |
| 33 |
Opts :: cowboy_websocket:opts() |
| 34 |
InFrame :: ping | pong | {text | binary | ping | pong, binary()} |
| 35 |
Info :: any() |
| 36 |
|
| 37 |
CallResult :: {commands(), State} |
| 38 |
| {commands(), State, hibernate} |
| 39 |
| Deprecated |
| 40 |
|
| 41 |
Deprecated :: {ok, State} |
| 42 |
| {ok, State, hibernate} |
| 43 |
| {reply, OutFrame | [OutFrame], State} |
| 44 |
| {reply, OutFrame | [OutFrame], State, hibernate} |
| 45 |
| {stop, State} |
| 46 |
OutFrame :: cow_ws:frame() %% see types below |
| 47 |
|
| 48 |
Reason :: normal | stop | timeout |
| 49 |
| remote | {remote, cow_ws:close_code(), binary()} |
| 50 |
| {error, badencoding | badframe | closed | atom()} |
| 51 |
| {crash, error | exit | throw, any()} |
| 52 |
---- |
| 53 |
|
| 54 |
The `init/2` callback is common to all handlers. To upgrade |
| 55 |
the connection to Websocket, it must return `cowboy_websocket` |
| 56 |
as the first element of the tuple. |
| 57 |
|
| 58 |
Any operation requiring the HTTP request must be done in the |
| 59 |
`init/2` function, as the Req object will not be available |
| 60 |
after it returns. Websocket sub-protocol selection should |
| 61 |
therefore be done in this function. |
| 62 |
|
| 63 |
The optional `websocket_init/1` callback will be called once |
| 64 |
the connection has been upgraded to Websocket. It can be used |
| 65 |
to perform any required initialization of the handler. |
| 66 |
|
| 67 |
Note that the `init/2` function does not run in the same |
| 68 |
process as the Websocket callbacks. Any Websocket-specific |
| 69 |
initialization must be done in `websocket_init/1`. |
| 70 |
|
| 71 |
The `websocket_handle/2` callback will be called for every |
| 72 |
frame received. The `websocket_info/2` callback will be |
| 73 |
called for every Erlang message received. |
| 74 |
|
| 75 |
All three Websocket callbacks may send one or more frames |
| 76 |
back to the client, including close frames to terminate |
| 77 |
the connection; enable/disable active mode; enable/disable |
| 78 |
compression for subsequent frames; or change Websocket options. |
| 79 |
|
| 80 |
The optional `terminate/3` callback will ultimately be called |
| 81 |
with the reason for the termination of the connection. This |
| 82 |
callback is common to all handlers. Note that Websocket will |
| 83 |
not provide the full Req object by default, to save memory. |
| 84 |
|
| 85 |
Cowboy will terminate the process right after closing the |
| 86 |
Websocket connection. This means that there is no need to |
| 87 |
perform any cleanup in the `terminate/3` callback. |
| 88 |
|
| 89 |
The following terminate reasons are defined for Websocket |
| 90 |
connections: |
| 91 |
|
| 92 |
normal:: |
| 93 |
The connection was closed normally before establishing a Websocket |
| 94 |
connection. This typically happens if an `ok` tuple is returned |
| 95 |
from the `init/2` callback. |
| 96 |
|
| 97 |
remote:: |
| 98 |
The remote endpoint closed the connection without giving any |
| 99 |
further details. |
| 100 |
|
| 101 |
{remote, Code, Payload}:: |
| 102 |
The remote endpoint closed the connection with the given |
| 103 |
`Code` and `Payload` as the reason. |
| 104 |
|
| 105 |
stop:: |
| 106 |
The handler requested to close the connection, either by returning |
| 107 |
a `stop` tuple or by sending a `close` frame. |
| 108 |
|
| 109 |
timeout:: |
| 110 |
The connection has been closed due to inactivity. The timeout |
| 111 |
value can be configured from `init/2`. |
| 112 |
|
| 113 |
{crash, Class, Reason}:: |
| 114 |
A crash occurred in the handler. `Class` and `Reason` can be |
| 115 |
used to obtain more information about the crash. |
| 116 |
|
| 117 |
{error, badencoding}:: |
| 118 |
A text frame was sent by the client with invalid encoding. All |
| 119 |
text frames must be valid UTF-8. |
| 120 |
|
| 121 |
{error, badframe}:: |
| 122 |
A protocol error has been detected. |
| 123 |
|
| 124 |
{error, closed}:: |
| 125 |
The socket has been closed brutally without a close frame being |
| 126 |
received first. |
| 127 |
|
| 128 |
{error, Reason}:: |
| 129 |
A socket error occurred. |
| 130 |
|
| 131 |
== Types |
| 132 |
|
| 133 |
=== commands() |
| 134 |
|
| 135 |
[source,erlang] |
| 136 |
---- |
| 137 |
commands() :: [Command] |
| 138 |
|
| 139 |
Command :: {active, boolean()} |
| 140 |
| {deflate, boolean()} |
| 141 |
| {set_options, #{ |
| 142 |
idle_timeout => timeout(), |
| 143 |
max_frame_size => non_neg_integer() | infinity}} |
| 144 |
| {shutdown_reason, any()} |
| 145 |
| Frame :: cow_ws:frame() |
| 146 |
---- |
| 147 |
|
| 148 |
Commands that may be returned from Websocket callbacks. |
| 149 |
|
| 150 |
The following commands are defined: |
| 151 |
|
| 152 |
active:: |
| 153 |
|
| 154 |
Whether to disable or enable reading from the socket. This |
| 155 |
can be used to apply flow control to a Websocket connection. |
| 156 |
|
| 157 |
deflate:: |
| 158 |
|
| 159 |
Whether the subsequent frames should be compressed. Has no |
| 160 |
effect on connections that did not negotiate compression. |
| 161 |
|
| 162 |
set_options:: |
| 163 |
|
| 164 |
Set Websocket options. Currently only the options `idle_timeout` |
| 165 |
and `max_frame_size` may be updated from a Websocket handler. |
| 166 |
|
| 167 |
shutdown_reason:: |
| 168 |
|
| 169 |
Change the shutdown reason. The Websocket process will exit |
| 170 |
with reason `normal` by default. This command can be used to |
| 171 |
exit with reason `{shutdown, ShutdownReason}` under normal |
| 172 |
conditions. This command has no effect when the Websocket |
| 173 |
process exits abnormally, for example following a crash in a |
| 174 |
handler callback. |
| 175 |
|
| 176 |
Frame:: |
| 177 |
|
| 178 |
Send the corresponding Websocket frame. |
| 179 |
|
| 180 |
=== cow_ws:frame() |
| 181 |
|
| 182 |
[source,erlang] |
| 183 |
---- |
| 184 |
frame() :: {text, iodata()} |
| 185 |
| {binary, iodata()} |
| 186 |
| ping | {ping, iodata()} |
| 187 |
| pong | {pong, iodata()} |
| 188 |
| close | {close, iodata()} | {close, close_code(), iodata()} |
| 189 |
|
| 190 |
close_code() :: 1000..1003 | 1006..1011 | 3000..4999 |
| 191 |
---- |
| 192 |
|
| 193 |
Websocket frames that can be sent as a response. |
| 194 |
|
| 195 |
Note that there is no need to send pong frames back as |
| 196 |
Cowboy does it automatically for you. |
| 197 |
|
| 198 |
=== opts() |
| 199 |
|
| 200 |
[source,erlang] |
| 201 |
---- |
| 202 |
opts() :: #{ |
| 203 |
active_n => pos_integer(), |
| 204 |
compress => boolean(), |
| 205 |
data_delivery => stream_handlers | relay, |
| 206 |
data_delivery_flow => pos_integer(), |
| 207 |
deflate_opts => cow_ws:deflate_opts(), |
| 208 |
dynamic_buffer => false | {pos_integer(), pos_integer()}, |
| 209 |
idle_timeout => timeout(), |
| 210 |
max_frame_size => non_neg_integer() | infinity, |
| 211 |
req_filter => fun((cowboy_req:req()) -> map()), |
| 212 |
validate_utf8 => boolean() |
| 213 |
} |
| 214 |
---- |
| 215 |
|
| 216 |
Websocket handler options. |
| 217 |
|
| 218 |
This configuration is passed to Cowboy from the `init/2` |
| 219 |
function: |
| 220 |
|
| 221 |
[source,erlang] |
| 222 |
---- |
| 223 |
init(Req, State) -> |
| 224 |
Opts = #{compress => true}, |
| 225 |
{cowboy_websocket, Req, State, Opts}. |
| 226 |
---- |
| 227 |
|
| 228 |
The default value is given next to the option name: |
| 229 |
|
| 230 |
active_n (1):: |
| 231 |
|
| 232 |
The number of packets Cowboy will request from the socket at once. |
| 233 |
This can be used to tweak the performance of the server. Higher |
| 234 |
values reduce the number of times Cowboy need to request more |
| 235 |
packets from the port driver at the expense of potentially |
| 236 |
higher memory being used. |
| 237 |
+ |
| 238 |
This option does not apply to Websocket over HTTP/2. |
| 239 |
|
| 240 |
compress (false):: |
| 241 |
|
| 242 |
Whether to enable the Websocket frame compression |
| 243 |
extension. Frames will only be compressed for the |
| 244 |
clients that support this extension. |
| 245 |
|
| 246 |
data_delivery (stream_handlers):: |
| 247 |
|
| 248 |
HTTP/2+ only. Determines how data will be delivered |
| 249 |
to the Websocket session process. `stream_handlers` |
| 250 |
is the default and makes data go through stream |
| 251 |
handlers. `relay` is a faster method introduced in |
| 252 |
Cowboy 2.14 and sends data directly. `relay` is |
| 253 |
intended to become the default in Cowboy 3.0. |
| 254 |
|
| 255 |
data_delivery_flow (131072):: |
| 256 |
|
| 257 |
When the `relay` data delivery method is used, |
| 258 |
this value may be used to decide how much the |
| 259 |
flow control window should be for the Websocket |
| 260 |
stream. Currently only applies to HTTP/2. |
| 261 |
|
| 262 |
deflate_opts (#{}):: |
| 263 |
|
| 264 |
Configuration for the permessage-deflate Websocket |
| 265 |
extension. Allows configuring both the negotiated |
| 266 |
options and the zlib compression options. The |
| 267 |
defaults optimize the compression at the expense |
| 268 |
of some memory and CPU. |
| 269 |
|
| 270 |
dynamic_buffer ({1024, 131072}):: |
| 271 |
|
| 272 |
Cowboy will dynamically change the socket's `buffer` size |
| 273 |
depending on the size of the data it receives from the socket. |
| 274 |
This lets Cowboy use the optimal buffer size for the current |
| 275 |
workload. |
| 276 |
+ |
| 277 |
The dynamic buffer size functionality can be disabled by |
| 278 |
setting this option to `false`. Cowboy will also disable |
| 279 |
it by default when the `buffer` transport option is configured. |
| 280 |
|
| 281 |
idle_timeout (60000):: |
| 282 |
|
| 283 |
Time in milliseconds that Cowboy will keep the |
| 284 |
connection open without receiving anything from |
| 285 |
the client. |
| 286 |
+ |
| 287 |
This option can be updated at any time using the |
| 288 |
`set_options` command. |
| 289 |
|
| 290 |
max_frame_size (1MB):: |
| 291 |
|
| 292 |
Maximum frame size in bytes allowed by this Websocket |
| 293 |
handler. Cowboy will close the connection when |
| 294 |
a client attempts to send a frame that goes over |
| 295 |
this limit. For fragmented frames this applies |
| 296 |
to the size of the reconstituted frame. |
| 297 |
|
| 298 |
req_filter:: |
| 299 |
|
| 300 |
A function applied to the Req to compact it and |
| 301 |
only keep required information. The Req is only |
| 302 |
given back in the `terminate/3` callback. By default |
| 303 |
it keeps the method, version, URI components and peer |
| 304 |
information. |
| 305 |
|
| 306 |
validate_utf8 (true):: |
| 307 |
|
| 308 |
Whether Cowboy should verify that the payload of |
| 309 |
`text` and `close` frames is valid UTF-8. This is |
| 310 |
required by the protocol specification but in some |
| 311 |
cases it may be more interesting to disable it in |
| 312 |
order to save resources. |
| 313 |
+ |
| 314 |
Note that `binary` frames do not have this UTF-8 |
| 315 |
requirement and are what should be used under |
| 316 |
normal circumstances if necessary. |
| 317 |
|
| 318 |
== Changelog |
| 319 |
|
| 320 |
* *2.17*: The `max_frame_size` option now defaults to 1MB. |
| 321 |
* *2.14*: The `data_delivery` and `data_delivery_flow` options |
| 322 |
were added. The `relay` data delivery mechanism |
| 323 |
provides a better way of forwarding data to |
| 324 |
HTTP/2+ Websocket session processes. |
| 325 |
* *2.13*: The `active_n` default value was changed to `1`. |
| 326 |
* *2.13*: The `dynamic_buffer` option was added. |
| 327 |
* *2.13*: The `max_frame_size` option can now be set dynamically. |
| 328 |
* *2.11*: Websocket over HTTP/2 is now considered stable. |
| 329 |
* *2.11*: HTTP/1.1 Websocket no longer traps exits by default. |
| 330 |
* *2.8*: The `active_n` option was added. |
| 331 |
* *2.7*: The commands based interface has been documented. |
| 332 |
The old interface is now deprecated. |
| 333 |
* *2.7*: The command `shutdown_reason` was introduced. |
| 334 |
* *2.7*: The option `validate_utf8` has been added. |
| 335 |
* *2.6*: Deflate options can now be configured via `deflate_opts`. |
| 336 |
* *2.0*: The Req object is no longer passed to Websocket callbacks. |
| 337 |
* *2.0*: The callback `websocket_terminate/3` was removed in favor of `terminate/3`. |
| 338 |
* *1.0*: Protocol introduced. |
| 339 |
|
| 340 |
== See also |
| 341 |
|
| 342 |
link:man:cowboy(7)[cowboy(7)], |
| 343 |
link:man:cowboy_handler(3)[cowboy_handler(3)], |
| 344 |
link:man:cowboy_http(3)[cowboy_http(3)], |
| 345 |
link:man:cowboy_http2(3)[cowboy_http2(3)] |