aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/cowboy_websocket_handler.ezdoc
blob: 0d31a548a683223845e5cee998e3456cbc37ba35 (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
::: cowboy_websocket_handler

The `cowboy_websocket_handler` behaviour defines the interface used
by Websocket handlers.

The `init/3` and `websocket_init/3` callbacks will always be called,
followed by zero or more calls to `websocket_handle/3` and
`websocket_info/3`. The `websocket_terminate/3` will always
be called last.

:: Types

None.

:: Callbacks

: init({TransportName, ProtocolName}, Req, Opts)
	-> {upgrade, protocol, cowboy_websocket}
	| {upgrade, protocol, cowboy_websocket, Req, Opts}

Types:

* TransportName = tcp | ssl | atom()
* ProtocolName = http | atom()
* Req = cowboy_req:req()
* Opts = any()

Upgrade the protocol to `cowboy_websocket`.

: websocket_init(TransportName, Req, Opts)
	-> {ok, Req, State}
	| {ok, Req, State, hibernate}
	| {ok, Req, State, Timeout}
	| {ok, Req, State, Timeout, hibernate}
	| {shutdown, Req}

Types:

* TransportName = tcp | ssl | atom()
* Req = cowboy_req:req()
* Opts = any()
* State = any()
* Timeout = timeout()

Initialize the state for this session.

This function is called before the upgrade to Websocket occurs.
It can be used to negotiate Websocket protocol extensions
with the client. It will typically be used to register this process
to an event manager or a message queue in order to receive
the messages the handler wants to process.

The connection will stay up for a duration of up to `Timeout`
milliseconds after it last received data from the socket,
at which point it will stop and close the connection.
By default this value is set to `infinity`. It is recommended
to either set this value or ensure by any other mechanism
that the handler will be closed after a certain period of
inactivity.

The `hibernate` option will hibernate the process until it
starts receiving either data from the Websocket connection
or Erlang messages.

The `shutdown` return value can be used to close the connection
before upgrading to Websocket.

: websocket_handle(InFrame, Req, State)
	-> {ok, Req, State}
	| {ok, Req, State, hibernate}
	| {reply, OutFrame | [OutFrame], Req, State}
	| {reply, OutFrame | [OutFrame], Req, State, hibernate}
	| {shutdown, Req, State}

Types:

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

Handle the data received from the Websocket connection.

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

The `shutdown` 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)
	-> {ok, Req, State}
	| {ok, Req, State, hibernate}
	| {reply, OutFrame | [OutFrame], Req, State}
	| {reply, OutFrame | [OutFrame], Req, State, hibernate}
	| {shutdown, Req, State}

Types:

* Info = any()
* Req = cowboy_req:req()
* State = any()
* OutFrame = cowboy_websocket: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 `shutdown` 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.

: websocket_terminate(Reason, Req, State) -> ok

Types:

* Reason = {normal, shutdown | timeout} | {remote, closed} | {remote, cowboy_websocket:close_code(), binary()} | {error, badencoding | badframe | closed | atom()}
* Req = cowboy_req:req()
* State = any()

Perform any necessary cleanup of the state.

The connection will be closed and the process stopped right
after this call.