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, PartialReq, State) -> ok %% optional
  20. Req :: cowboy_req:req()
  21. PartialReq :: map()
  22. State :: any()
  23. Opts :: cowboy_websocket:opts()
  24. InFrame :: {text | binary | ping | pong, binary()}
  25. OutFrame :: cow_ws:frame()
  26. Info :: any()
  27. CallResult :: {ok, State}
  28. | {ok, State, hibernate}
  29. | {reply, OutFrame | [OutFrame], State}
  30. | {reply, OutFrame | [OutFrame], State, hibernate}
  31. | {stop, State}
  32. Reason :: normal | stop | timeout
  33. | remote | {remote, cow_ws:close_code(), binary()}
  34. | {error, badencoding | badframe | closed | atom()}
  35. | {crash, error | exit | throw, any()}
  36. ----
  37. The `init/2` callback is common to all handlers. To upgrade
  38. the connection to Websocket, it must return `cowboy_websocket`
  39. as the first element of the tuple.
  40. Any operation requiring the HTTP request must be done in the
  41. `init/2` function, as the Req object will not be available
  42. after it returns. Websocket sub-protocol selection should
  43. therefore be done in this function.
  44. The optional `websocket_init/1` callback will be called once
  45. the connection has been upgraded to Websocket. It can be used
  46. to perform any required initialization of the handler.
  47. Note that the `init/2` function does not run in the same
  48. process as the Websocket callbacks. Any Websocket-specific
  49. initialization must be done in `websocket_init/1`.
  50. The `websocket_handle/2` callback will be called for every
  51. frame received. The `websocket_info/2` callback will be
  52. called for every Erlang message received.
  53. All three Websocket callbacks may send one or more frames
  54. back to the client (by returning a `reply` tuple) or terminate
  55. the connection (by sending a `close` frame or returning a `stop`
  56. tuple).
  57. The optional `terminate/3` callback will ultimately be called
  58. with the reason for the termination of the connection. This
  59. callback is common to all handlers. Note that Websocket will
  60. not provide the full Req object by default, to save memory.
  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)]