cowboy.start_clear.asciidoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. = cowboy:start_clear(3)
  2. == Name
  3. cowboy:start_clear - Listen for connections using plain TCP
  4. == Description
  5. [source,erlang]
  6. ----
  7. start_clear(Name :: ranch:ref(),
  8. TransportOpts :: ranch_tcp:opts(),
  9. ProtocolOpts :: opts())
  10. -> {ok, ListenerPid :: pid()}
  11. | {error, any()}
  12. ----
  13. Start listening for connections over a clear TCP channel.
  14. Both HTTP/1.1 and HTTP/2 are supported on this listener.
  15. HTTP/2 has two methods of establishing a connection over
  16. a clear TCP channel. Both the upgrade and the prior knowledge
  17. methods are supported.
  18. == Arguments
  19. Name::
  20. The listener name is used to refer to this listener in
  21. future calls, for example when stopping it or when
  22. updating the routes defined.
  23. +
  24. It can be any Erlang term. An atom is generally good enough,
  25. for example `api`, `my_app_clear` or `my_app_tls`.
  26. TransportOpts::
  27. The transport options are where the TCP options, including
  28. the listener's port number, are defined. Transport options
  29. are provided as a list of keys and values, for example
  30. `[{port, 8080}]`.
  31. +
  32. The available options are documented in the
  33. link:man:ranch_tcp(3)[ranch_tcp(3)] manual.
  34. ProtocolOpts::
  35. The protocol options are in a map containing all the options for
  36. the different protocols that may be involved when connecting
  37. to the listener, including HTTP/1.1 and HTTP/2 but also
  38. subprotocols like Websocket.
  39. // @todo For Websocket this might change in the future.
  40. +
  41. The HTTP/1.1 options are documented in the
  42. link:man:cowboy_http(3)[cowboy_http(3)] manual;
  43. the HTTP/2 options in
  44. link:man:cowboy_http2(3)[cowboy_http2(3)];
  45. and the Websocket options in
  46. link:man:cowboy_websocket(3)[cowboy_websocket(3)].
  47. == Return value
  48. An ok tuple is returned on success. It contains the pid of
  49. the top-level supervisor for the listener.
  50. An error tuple is returned on error. The error reason may
  51. be any Erlang term.
  52. A common error is `eaddrinuse`. It indicates that the port
  53. configured for Cowboy is already in use.
  54. == Changelog
  55. * *2.0*: HTTP/2 support added.
  56. * *2.0*: Function introduced. Replaces `cowboy:start_http/4`.
  57. == Examples
  58. .Start a listener
  59. [source,erlang]
  60. ----
  61. Dispatch = cowboy_router:compile([
  62. {'_', [
  63. {"/", toppage_h, []}
  64. ]}
  65. ]),
  66. {ok, _} = cowboy:start_clear(example, [{port, 8080}], #{
  67. env => #{dispatch => Dispatch}
  68. }).
  69. ----
  70. .Start a listener on a random port
  71. [source,erlang]
  72. ----
  73. Name = example,
  74. {ok, _} = cowboy:start_clear(Name, [], #{
  75. env => #{dispatch => Dispatch}
  76. }),
  77. Port = ranch:get_port(Name).
  78. ----
  79. == See also
  80. link:man:cowboy(3)[cowboy(3)],
  81. link:man:cowboy:start_tls(3)[cowboy:start_tls(3)],
  82. link:man:cowboy:stop_listener(3)[cowboy:stop_listener(3)],
  83. link:man:ranch(3)[ranch(3)]