summaryrefslogtreecommitdiffstats
path: root/docs/en/cowboy/2.0/manual/cowboy_websocket/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/en/cowboy/2.0/manual/cowboy_websocket/index.html')
-rw-r--r--docs/en/cowboy/2.0/manual/cowboy_websocket/index.html239
1 files changed, 135 insertions, 104 deletions
diff --git a/docs/en/cowboy/2.0/manual/cowboy_websocket/index.html b/docs/en/cowboy/2.0/manual/cowboy_websocket/index.html
index 8274fbf8..2ca978a3 100644
--- a/docs/en/cowboy/2.0/manual/cowboy_websocket/index.html
+++ b/docs/en/cowboy/2.0/manual/cowboy_websocket/index.html
@@ -72,68 +72,126 @@
<div class="sect1">
<h2 id="_name">Name</h2>
<div class="sectionbody">
-<div class="paragraph"><p>cowboy_websocket - Websocket protocol</p></div>
+<div class="paragraph"><p>cowboy_websocket - Websocket</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
-<div class="paragraph"><p>The <code>cowboy_websocket</code> module implements the Websocket protocol.</p></div>
-<div class="paragraph"><p>This module is a sub protocol that defines four callbacks to
-be implemented by handlers. The <code>init/2</code> and <code>terminate/3</code>
-callbacks are common to all handler types and are documented
-in the manual for the <a href="cowboy_handler.asciidoc">cowboy_handler</a> module.</p></div>
-<div class="paragraph"><p>The <code>websocket_handle/2</code> and <code>websocket_info/2</code> callbacks are
-specific to Websocket handlers and will be called as many times
-as necessary until the Websocket connection is closed.</p></div>
-<div class="paragraph"><p>The <code>init/2</code> 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 <code>infinity</code>, which should only be used if
-you have alternate means of ending inactive connections.</p></div>
-<div class="paragraph"><p>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 <code>terminate/3</code> callback.</p></div>
+<div class="paragraph"><p>The module <code>cowboy_websocket</code> implements Websocket
+as a Ranch protocol. It also defines a callback interface
+for handling Websocket connections.</p></div>
</div>
</div>
<div class="sect1">
-<h2 id="_meta_values">Meta values</h2>
+<h2 id="_options">Options</h2>
<div class="sectionbody">
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 3.1.8
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000000">opts</span></span>() <span style="color: #990000">::</span> #{
+ <span style="color: #FF6600">websocket_compress</span> <span style="color: #990000">:=</span> <span style="font-weight: bold"><span style="color: #000000">boolean</span></span>()
+}</tt></pre></div></div>
+<div class="paragraph"><p>Configuration for the Websocket protocol.</p></div>
+<div class="paragraph"><p>This configuration is passed to Cowboy when starting listeners
+using <code>cowboy:start_clear/4</code> or <code>cowboy:start_tls/4</code> functions.</p></div>
+<div class="paragraph"><p>It can be updated without restarting listeners using the
+Ranch functions <code>ranch:get_protocol_options/1</code> and
+<code>ranch:set_protocol_options/2</code>.</p></div>
+<div class="paragraph"><p>The default value is given next to the option name:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
-websocket_compress = boolean()
-</dt>
-<dd>
-<p>
- Whether a websocket compression extension in in use.
-</p>
-</dd>
-<dt class="hdlist1">
-websocket_version = 7 | 8 | 13
+websocket_compress (false)
</dt>
<dd>
<p>
- The version of the Websocket protocol being used.
+ Whether to enable the Websocket frame compression
+ extension. Frames will only be compressed for the
+ clients that support this extension.
</p>
</dd>
</dl></div>
</div>
</div>
<div class="sect1">
-<h2 id="_terminate_reasons">Terminate reasons</h2>
+<h2 id="_callbacks">Callbacks</h2>
<div class="sectionbody">
-<div class="paragraph"><p>The following values may be received as the terminate reason
-in the optional <code>terminate/3</code> callback.</p></div>
+<div class="paragraph"><p>Websocket handlers must implement the following callback
+interface:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 3.1.8
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000000">init</span></span>(<span style="color: #009900">Req</span>, <span style="color: #009900">State</span>)
+ <span style="color: #990000">-&gt;</span> {<span style="color: #FF6600">cowboy_websocket</span>, <span style="color: #009900">Req</span>, <span style="color: #009900">State</span>}
+ | {<span style="color: #FF6600">cowboy_websocket</span>, <span style="color: #009900">Req</span>, <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
+ | {<span style="color: #FF6600">cowboy_websocket</span>, <span style="color: #009900">Req</span>, <span style="color: #009900">State</span>, <span style="font-weight: bold"><span style="color: #000000">timeout</span></span>()}
+ | {<span style="color: #FF6600">cowboy_websocket</span>, <span style="color: #009900">Req</span>, <span style="color: #009900">State</span>, <span style="font-weight: bold"><span style="color: #000000">timeout</span></span>(), <span style="color: #FF6600">hibernate</span>}
+
+<span style="font-weight: bold"><span style="color: #000000">websocket_init</span></span>(<span style="color: #009900">State</span>) <span style="color: #990000">-&gt;</span> <span style="color: #009900">CallResult</span> <span style="font-style: italic"><span style="color: #9A1900">%% optional</span></span>
+<span style="font-weight: bold"><span style="color: #000000">websocket_handle</span></span>(<span style="color: #009900">InFrame</span>, <span style="color: #009900">State</span>) <span style="color: #990000">-&gt;</span> <span style="color: #009900">CallResult</span>
+<span style="font-weight: bold"><span style="color: #000000">websocket_info</span></span>(<span style="color: #009900">Info</span>, <span style="color: #009900">State</span>) <span style="color: #990000">-&gt;</span> <span style="color: #009900">CallResult</span>
+
+<span style="font-weight: bold"><span style="color: #000000">terminate</span></span>(<span style="color: #009900">Reason</span>, <span style="color: #000080">undefined</span>, <span style="color: #009900">State</span>) <span style="color: #990000">-&gt;</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>() <span style="font-style: italic"><span style="color: #9A1900">%% optional</span></span>
+
+<span style="color: #009900">Req</span> <span style="color: #990000">::</span> <span style="font-weight: bold"><span style="color: #000000">cowboy_req:req</span></span>()
+<span style="color: #009900">State</span> <span style="color: #990000">::</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>()
+<span style="color: #009900">InFrame</span> <span style="color: #990000">::</span> {<span style="color: #FF6600">text</span> | <span style="font-weight: bold"><span style="color: #000080">binary</span></span> | <span style="color: #FF6600">ping</span> | <span style="color: #FF6600">pong</span>, <span style="font-weight: bold"><span style="color: #000080">binary</span></span>()}
+<span style="color: #009900">OutFrame</span> <span style="color: #990000">::</span> <span style="font-weight: bold"><span style="color: #000000">cow_ws:frame</span></span>()
+<span style="color: #009900">Info</span> <span style="color: #990000">::</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>()
+
+<span style="color: #009900">CallResult</span> <span style="color: #990000">::</span> {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>}
+ | {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
+ | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>}
+ | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
+ | {<span style="color: #FF6600">stop</span>, <span style="color: #009900">State</span>}
+
+<span style="color: #009900">Reason</span> <span style="color: #990000">::</span> <span style="font-weight: bold"><span style="color: #000080">normal</span></span> | <span style="color: #FF6600">stop</span> | <span style="color: #FF6600">timeout</span>
+ | <span style="color: #FF6600">remote</span> | {<span style="color: #FF6600">remote</span>, <span style="font-weight: bold"><span style="color: #000000">cow_ws:close_code</span></span>(), <span style="font-weight: bold"><span style="color: #000080">binary</span></span>()}
+ | {<span style="color: #FF6600">error</span>, <span style="color: #FF6600">badencoding</span> | <span style="color: #FF6600">badframe</span> | <span style="color: #FF6600">closed</span> | <span style="font-weight: bold"><span style="color: #000080">atom</span></span>()}
+ | {<span style="color: #FF6600">crash</span>, <span style="color: #FF6600">error</span> | <span style="font-weight: bold"><span style="color: #000080">exit</span></span> | <span style="font-weight: bold"><span style="color: #000080">throw</span></span>, <span style="font-weight: bold"><span style="color: #000000">any</span></span>()}</tt></pre></div></div>
+<div class="paragraph"><p>The <code>init/2</code> callback is common to all handlers. To upgrade
+the connection to Websocket, it must return <code>cowboy_websocket</code>
+as the first element of the tuple.</p></div>
+<div class="paragraph"><p>Any operation requiring the HTTP request must be done in the
+<code>init/2</code> function, as the Req object will not be available
+after it returns. Websocket sub-protocol selection should
+therefore be done in this function.</p></div>
+<div class="paragraph"><p>The optional <code>websocket_init/1</code> callback will be called once
+the connection has been upgraded to Websocket. It can be used
+to perform any required initialization of the handler.</p></div>
+<div class="paragraph"><p>Note that the <code>init/2</code> function does not run in the same
+process as the Websocket callbacks. Any Websocket-specific
+initialization must be done in <code>websocket_init/1</code>.</p></div>
+<div class="paragraph"><p>The <code>websocket_handle/2</code> callback will be called for every
+frame received. The <code>websocket_info/2</code> callback will be
+called for every Erlang message received.</p></div>
+<div class="paragraph"><p>All three Websocket callbacks may send one or more frames
+back to the client (by returning a <code>reply</code> tuple) or terminate
+the connection (by sending a <code>close</code> frame or returning a <code>stop</code>
+tuple).</p></div>
+<div class="paragraph"><p>The optional <code>terminate/3</code> callback will ultimately be called
+with the reason for the termination of the connection. This
+callback is common to all handlers. Note that Websocket has
+no concept of requests so it sets the second argument to
+undefined.</p></div>
+<div class="paragraph"><p>Cowboy will terminate the process right after closing the
+Websocket connection. This means that there is no need to
+perform any cleanup in the <code>terminate/3</code> callback.</p></div>
+<div class="paragraph"><p>The following terminate reasons are defined for Websocket
+connections:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
normal
</dt>
<dd>
<p>
- The connection was closed normally before establishing a Websocket
- connection. This typically happens if an <code>ok</code> tuple is returned
- from the <code>init/2</code> callback.
+ The connection was closed normally before establishing a Websocket
+ connection. This typically happens if an <code>ok</code> tuple is returned
+ from the <code>init/2</code> callback.
</p>
</dd>
<dt class="hdlist1">
@@ -141,8 +199,8 @@ remote
</dt>
<dd>
<p>
- The remote endpoint closed the connection without giving any
- further details.
+ The remote endpoint closed the connection without giving any
+ further details.
</p>
</dd>
<dt class="hdlist1">
@@ -150,8 +208,8 @@ remote
</dt>
<dd>
<p>
- The remote endpoint closed the connection with the given
- <code>Code</code> and <code>Payload</code> as the reason.
+ The remote endpoint closed the connection with the given
+ <code>Code</code> and <code>Payload</code> as the reason.
</p>
</dd>
<dt class="hdlist1">
@@ -159,8 +217,8 @@ stop
</dt>
<dd>
<p>
- The handler requested to close the connection, either by returning
- a <code>stop</code> tuple or by sending a <code>close</code> frame.
+ The handler requested to close the connection, either by returning
+ a <code>stop</code> tuple or by sending a <code>close</code> frame.
</p>
</dd>
<dt class="hdlist1">
@@ -168,8 +226,8 @@ timeout
</dt>
<dd>
<p>
- The connection has been closed due to inactivity. The timeout
- value can be configured from <code>init/2</code>.
+ The connection has been closed due to inactivity. The timeout
+ value can be configured from <code>init/2</code>.
</p>
</dd>
<dt class="hdlist1">
@@ -177,10 +235,10 @@ timeout
</dt>
<dd>
<p>
- A crash occurred in the handler. <code>Class</code> and <code>Reason</code> can be
- used to obtain more information about the crash. The function
- <code>erlang:get_stacktrace/0</code> can also be called to obtain the
- stacktrace of the process when the crash occurred.
+ A crash occurred in the handler. <code>Class</code> and <code>Reason</code> can be
+ used to obtain more information about the crash. The function
+ <code>erlang:get_stacktrace/0</code> can also be called to obtain the
+ stacktrace of the process when the crash occurred.
</p>
</dd>
<dt class="hdlist1">
@@ -188,8 +246,8 @@ timeout
</dt>
<dd>
<p>
- A text frame was sent by the client with invalid encoding. All
- text frames must be valid UTF-8.
+ A text frame was sent by the client with invalid encoding. All
+ text frames must be valid UTF-8.
</p>
</dd>
<dt class="hdlist1">
@@ -197,7 +255,7 @@ timeout
</dt>
<dd>
<p>
- A protocol error has been detected.
+ A protocol error has been detected.
</p>
</dd>
<dt class="hdlist1">
@@ -205,8 +263,8 @@ timeout
</dt>
<dd>
<p>
- The socket has been closed brutally without a close frame being
- received first.
+ The socket has been closed brutally without a close frame being
+ received first.
</p>
</dd>
<dt class="hdlist1">
@@ -214,67 +272,40 @@ timeout
</dt>
<dd>
<p>
- A socket error ocurred.
+ A socket error ocurred.
</p>
</dd>
</dl></div>
</div>
</div>
<div class="sect1">
-<h2 id="_callbacks">Callbacks</h2>
+<h2 id="_changelog">Changelog</h2>
<div class="sectionbody">
-<div class="sect2">
-<h3 id="_websocket_handle_inframe_state_8594_ret">websocket_handle(InFrame, State) &#8594; Ret</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 3.1.8
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">Ret</span> <span style="color: #990000">=</span> {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>}
- | {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
- | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>}
- | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
- | {<span style="color: #FF6600">stop</span>, <span style="color: #009900">State</span>}
-
-<span style="color: #009900">InFrame</span> <span style="color: #990000">=</span> {<span style="color: #FF6600">text</span> | <span style="font-weight: bold"><span style="color: #000080">binary</span></span> | <span style="color: #FF6600">ping</span> | <span style="color: #FF6600">pong</span>, <span style="font-weight: bold"><span style="color: #000080">binary</span></span>()}
-<span style="color: #009900">State</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>()
-<span style="color: #009900">OutFrame</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cow_ws:frame</span></span>()</tt></pre></div></div>
-<div class="paragraph"><p>Handle the data received from the Websocket connection.</p></div>
-<div class="paragraph"><p>This function will be called every time data is received
-from the Websocket connection.</p></div>
-<div class="paragraph"><p>The <code>stop</code> return value can be used to close the
-connection. A close reply will also result in the connection
-being closed.</p></div>
-<div class="paragraph"><p>The <code>hibernate</code> option will hibernate the process until
-it receives new data from the Websocket connection or an
-Erlang message.</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<strong>2.0</strong>: The Req object is no longer passed to Websocket callbacks.
+</p>
+</li>
+<li>
+<p>
+<strong>2.0</strong>: The callback <code>websocket_terminate/3</code> was removed in favor of <code>terminate/3</code>.
+</p>
+</li>
+<li>
+<p>
+<strong>1.0</strong>: Protocol introduced.
+</p>
+</li>
+</ul></div>
</div>
-<div class="sect2">
-<h3 id="_websocket_info_info_state_8594_ret">websocket_info(Info, State) &#8594; Ret</h3>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 3.1.8
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">Ret</span> <span style="color: #990000">=</span> {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>}
- | {<span style="color: #FF6600">ok</span>, <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
- | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>}
- | {<span style="color: #FF6600">reply</span>, <span style="color: #009900">OutFrame</span> | [<span style="color: #009900">OutFrame</span>], <span style="color: #009900">State</span>, <span style="color: #FF6600">hibernate</span>}
- | {<span style="color: #FF6600">stop</span>, <span style="color: #009900">State</span>}
-
-<span style="color: #009900">Info</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>()
-<span style="color: #009900">State</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">any</span></span>()
-<span style="color: #009900">OutFrame</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">cow_ws:frame</span></span>()</tt></pre></div></div>
-<div class="paragraph"><p>Handle the Erlang message received.</p></div>
-<div class="paragraph"><p>This function will be called every time an Erlang message
-has been received. The message can be any Erlang term.</p></div>
-<div class="paragraph"><p>The <code>stop</code> return value can be used to close the
-connection. A close reply will also result in the connection
-being closed.</p></div>
-<div class="paragraph"><p>The <code>hibernate</code> option will hibernate the process until
-it receives another message or new data from the Websocket
-connection.</p></div>
</div>
+<div class="sect1">
+<h2 id="_see_also">See also</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><a href="../cowboy_app">cowboy(7)</a>,
+<a href="../cowboy_http">cowboy_http(3)</a>,
+<a href="../cowboy_http">cowboy_http2(3)</a></p></div>
</div>
</div>