cowboy_websocket.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. = cowboy_websocket(3)
  2. == Name
  3. cowboy_websocket - Websocket
  4. == Description
  5. The module `cowboy_websocket` implements Websocket
  6. as a Ranch protocol. It also defines a callback interface
  7. for handling Websocket connections.
  8. == Callbacks
  9. Websocket handlers must implement the following callback
  10. interface:
  11. [source,erlang]
  12. ----
  13. init(Req, State)
  14. -> {cowboy_websocket, Req, State}
  15. | {cowboy_websocket, Req, State, Opts}
  16. websocket_init(State) -> CallResult %% optional
  17. websocket_handle(InFrame, State) -> CallResult
  18. websocket_info(Info, State) -> CallResult
  19. terminate(Reason, undefined, State) -> ok %% optional
  20. Req :: cowboy_req:req()
  21. State :: any()
  22. Opts :: cowboy_websocket:opts()
  23. InFrame :: {text | binary | ping | pong, binary()}
  24. OutFrame :: cow_ws:frame()
  25. Info :: any()
  26. CallResult :: {ok, State}
  27. | {ok, State, hibernate}
  28. | {reply, OutFrame | [OutFrame], State}
  29. | {reply, OutFrame | [OutFrame], State, hibernate}
  30. | {stop, State}
  31. Reason :: normal | stop | timeout
  32. | remote | {remote, cow_ws:close_code(), binary()}
  33. | {error, badencoding | badframe | closed | atom()}
  34. | {crash, error | exit | throw, any()}
  35. ----
  36. The `init/2` callback is common to all handlers. To upgrade
  37. the connection to Websocket, it must return `cowboy_websocket`
  38. as the first element of the tuple.
  39. Any operation requiring the HTTP request must be done in the
  40. `init/2` function, as the Req object will not be available
  41. after it returns. Websocket sub-protocol selection should
  42. therefore be done in this function.
  43. The optional `websocket_init/1` callback will be called once
  44. the connection has been upgraded to Websocket. It can be used
  45. to perform any required initialization of the handler.
  46. Note that the `init/2` function does not run in the same
  47. process as the Websocket callbacks. Any Websocket-specific
  48. initialization must be done in `websocket_init/1`.
  49. The `websocket_handle/2` callback will be called for every
  50. frame received. The `websocket_info/2` callback will be
  51. called for every Erlang message received.
  52. All three Websocket callbacks may send one or more frames
  53. back to the client (by returning a `reply` tuple) or terminate
  54. the connection (by sending a `close` frame or returning a `stop`
  55. tuple).
  56. The optional `terminate/3` callback will ultimately be called
  57. with the reason for the termination of the connection. This
  58. callback is common to all handlers. Note that Websocket has
  59. no concept of requests so it sets the second argument to
  60. undefined.
  61. Cowboy will terminate the process right after closing the
  62. Websocket connection. This means that there is no need to
  63. perform any cleanup in the `terminate/3` callback.
  64. The following terminate reasons are defined for Websocket
  65. connections:
  66. normal::
  67. The connection was closed normally before establishing a Websocket
  68. connection. This typically happens if an `ok` tuple is returned
  69. from the `init/2` callback.
  70. remote::
  71. The remote endpoint closed the connection without giving any
  72. further details.
  73. {remote, Code, Payload}::
  74. The remote endpoint closed the connection with the given
  75. `Code` and `Payload` as the reason.
  76. stop::
  77. The handler requested to close the connection, either by returning
  78. a `stop` tuple or by sending a `close` frame.
  79. timeout::
  80. The connection has been closed due to inactivity. The timeout
  81. value can be configured from `init/2`.
  82. {crash, Class, Reason}::
  83. A crash occurred in the handler. `Class` and `Reason` can be
  84. used to obtain more information about the crash. The function
  85. `erlang:get_stacktrace/0` can also be called to obtain the
  86. stacktrace of the process when the crash occurred.
  87. {error, badencoding}::
  88. A text frame was sent by the client with invalid encoding. All
  89. text frames must be valid UTF-8.
  90. {error, badframe}::
  91. A protocol error has been detected.
  92. {error, closed}::
  93. The socket has been closed brutally without a close frame being
  94. received first.
  95. {error, Reason}::
  96. A socket error ocurred.
  97. == Types
  98. === opts()
  99. [source,erlang]
  100. ----
  101. opts() :: #{
  102. compress => boolean(),
  103. idle_timeout => timeout(),
  104. req_filter => fun((cowboy_req:req()) -> map())
  105. }
  106. ----
  107. Websocket handler options.
  108. This configuration is passed to Cowboy from the `init/2`
  109. function:
  110. [source,erlang]
  111. ----
  112. init(Req, State) ->
  113. Opts = #{compress => true},
  114. {cowboy_websocket, Req, State, Opts}.
  115. ----
  116. The default value is given next to the option name:
  117. compress (false)::
  118. Whether to enable the Websocket frame compression
  119. extension. Frames will only be compressed for the
  120. clients that support this extension.
  121. idle_timeout (60000)::
  122. Time in milliseconds that Cowboy will keep the
  123. connection open without receiving anything from
  124. the client.
  125. req_filter::
  126. A function applied to the Req to compact it and
  127. only keep required information. The Req is only
  128. given back in the `terminate/3` callback. By default
  129. it keeps the method, version, URI components and peer
  130. information.
  131. == Changelog
  132. * *2.0*: The Req object is no longer passed to Websocket callbacks.
  133. * *2.0*: The callback `websocket_terminate/3` was removed in favor of `terminate/3`.
  134. * *1.0*: Protocol introduced.
  135. == See also
  136. link:man:cowboy(7)[cowboy(7)],
  137. link:man:cowboy_handler(3)[cowboy_handler(3)],
  138. link:man:cowboy_http(3)[cowboy_http(3)],
  139. link:man:cowboy_http2(3)[cowboy_http2(3)]