aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/cowboy_websocket.asciidoc
blob: ac9016be6b3113341d6fdaba7a4d76fca7622787 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
= cowboy_websocket(3)

== Name

cowboy_websocket - Websocket protocol

== Description

The `cowboy_websocket` module implements the Websocket protocol.

This module is a sub protocol that defines four callbacks to
be implemented by handlers. The `init/2` and `terminate/3`
callbacks are common to all handler types and are documented
in the manual for the link:cowboy_handler.asciidoc[cowboy_handler] module.

The `websocket_handle/3` and `websocket_info/3` callbacks are
specific to Websocket handlers and will be called as many times
as necessary until the Websocket connection is closed.

The `init/2` callback can be used to negotiate Websocket protocol
extensions with the client. It is highly recommended to return a
timeout value from this callback to ensure that the process is
terminated when no data has been received during that timespan.
The default timeout is `infinity`, which should only be used if
you have alternate means of ending inactive connections.

Cowboy will terminate the process right after closing the
Websocket connection. This means that there is no real need to
perform any cleanup in the optional `terminate/3` callback.

== Meta values

websocket_compress = boolean()::
	Whether a websocket compression extension in in use.

websocket_version = 7 | 8 | 13::
	The version of the Websocket protocol being used.

== Terminate reasons

The following values may be received as the terminate reason
in the optional `terminate/3` callback.

normal::
	The connection was closed normally before establishing a Websocket
	connection. This typically happens if an `ok` tuple is returned
	from the `init/2` callback.

remote::
	The remote endpoint closed the connection without giving any
	further details.

{remote, Code, Payload}::
	The remote endpoint closed the connection with the given
	`Code` and `Payload` as the reason.

stop::
	The handler requested to close the connection, either by returning
	a `stop` tuple or by sending a `close` frame.

timeout::
	The connection has been closed due to inactivity. The timeout
	value can be configured from `init/2`.

{crash, Class, Reason}::
	A crash occurred in the handler. `Class` and `Reason` can be
	used to obtain more information about the crash. The function
	`erlang:get_stacktrace/0` can also be called to obtain the
	stacktrace of the process when the crash occurred.

{error, badencoding}::
	A text frame was sent by the client with invalid encoding. All
	text frames must be valid UTF-8.

{error, badframe}::
	A protocol error has been detected.

{error, closed}::
	The socket has been closed brutally without a close frame being
	received first.

{error, Reason}::
	A socket error ocurred.

== Callbacks

=== websocket_handle(InFrame, Req, State) -> Ret

[source,erlang]
----
Ret = {ok, Req, State}
	| {ok, Req, State, hibernate}
	| {reply, OutFrame | [OutFrame], Req, State}
	| {reply, OutFrame | [OutFrame], Req, State, hibernate}
	| {stop, Req, State}

InFrame = {text | binary | ping | pong, binary()}
Req = cowboy_req:req()
State = any()
OutFrame = cow_ws:frame()
----

Handle the data received from the Websocket connection.

This function will be called every time data is received
from the Websocket connection.

The `stop` return value can be used to close the
connection. A close reply will also result in the connection
being closed.

The `hibernate` option will hibernate the process until
it receives new data from the Websocket connection or an
Erlang message.

=== websocket_info(Info, Req, State) -> Ret

[source,erlang]
----
Ret = {ok, Req, State}
	| {ok, Req, State, hibernate}
	| {reply, OutFrame | [OutFrame], Req, State}
	| {reply, OutFrame | [OutFrame], Req, State, hibernate}
	| {stop, Req, State}

Info = any()
Req = cowboy_req:req()
State = any()
OutFrame = cow_ws:frame()
----

Handle the Erlang message received.

This function will be called every time an Erlang message
has been received. The message can be any Erlang term.

The `stop` return value can be used to close the
connection. A close reply will also result in the connection
being closed.

The `hibernate` option will hibernate the process until
it receives another message or new data from the Websocket
connection.