libcurl-ws.3 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. .\" generated by cd2nroff 0.1 from libcurl-ws.md
  2. .TH libcurl-ws 3 "2025-01-17" libcurl
  3. .SH NAME
  4. libcurl\-ws \- WebSocket interface overview
  5. .SH DESCRIPTION
  6. The WebSocket interface provides functions for receiving and sending WebSocket
  7. data.
  8. .SH INCLUDE
  9. You still only include <curl/curl.h> in your code.
  10. .SH SETUP
  11. WebSocket is also often known as \fIWebSockets\fP, in plural. It is done by
  12. upgrading a regular HTTP(S) GET request to a WebSocket connection.
  13. WebSocket is a TCP\-like message\-based communication protocol done over HTTP,
  14. specified in RFC 6455.
  15. To initiate a WebSocket session with libcurl, setup an easy handle to use a
  16. URL with a "WS://" or "WSS://" scheme. "WS" is for cleartext communication
  17. over HTTP and "WSS" is for doing WebSocket securely over HTTPS.
  18. A WebSocket request is done as an HTTP/1 GET request with an "Upgrade
  19. WebSocket" request header field. When the upgrade is accepted by the server,
  20. it responds with a 101 Switching and then the client can speak WebSocket with
  21. the server. The communication can happen in both directions at the same time.
  22. .SH MESSAGES
  23. WebSocket communication is message based. That means that both ends send and
  24. receive entire messages, not streams like TCP. A WebSocket message is sent
  25. over the wire in one or more frames. Each frame in a message can have a size
  26. up to 2^63 bytes.
  27. libcurl delivers WebSocket data as frame fragments. It might send a whole
  28. frame, but it might also deliver them in pieces depending on size and network
  29. patterns. It makes sure to provide the API user about the exact specifics
  30. about the fragment: type, offset, size and how much data there is pending to
  31. arrive for the same frame.
  32. A message has an unknown size until the last frame header for the message has
  33. been received since only frames have set sizes.
  34. .SH Raw mode
  35. libcurl can be told to speak WebSocket in "raw mode" by setting the
  36. \fBCURLWS_RAW_MODE\fP bit to the \fICURLOPT_WS_OPTIONS(3)\fP option.
  37. Raw WebSocket means that libcurl passes on the data from the network without
  38. parsing it leaving that entirely to the application. This mode assumes that
  39. the user of this knows WebSocket and can parse and figure out the data all by
  40. itself.
  41. This mode is intended for applications that already have a WebSocket
  42. parser/engine that want to switch over to use libcurl for enabling WebSocket,
  43. and keep parts of the existing software architecture.
  44. .SH PING
  45. WebSocket is designed to allow long\-lived sessions and in order to keep the
  46. connections alive, both ends can send PING messages for the other end to
  47. respond with a PONG.
  48. libcurl automatically responds to server PING messages with a PONG. It does
  49. not send any PING messages automatically.
  50. .SH MODELS
  51. Because of the many different ways WebSocket can be used, which is much more
  52. flexible than limited to plain downloads or uploads, libcurl offers two
  53. different API models to use it:
  54. 1. Using a write callback with \fICURLOPT_WRITEFUNCTION(3)\fP much like other
  55. downloads for when the traffic is download oriented.
  56. 2. Using \fICURLOPT_CONNECT_ONLY(3)\fP and use the WebSocket recv/send
  57. functions.
  58. .SH Callback model
  59. When a write callback is set and a WebSocket transfer is performed, the
  60. callback is called to deliver all WebSocket data that arrives.
  61. The callback can then call \fIcurl_ws_meta(3)\fP to learn about the details of
  62. the incoming data fragment.
  63. .SH CONNECT_ONLY model
  64. By setting \fICURLOPT_CONNECT_ONLY(3)\fP to \fB2L\fP, the transfer only
  65. establishes and setups the WebSocket communication and then returns control
  66. back to the application.
  67. Once such a setup has been successfully performed, the application can proceed
  68. and use \fIcurl_ws_recv(3)\fP and \fIcurl_ws_send(3)\fP freely to exchange
  69. WebSocket messages with the server.
  70. .SH EXPERIMENTAL
  71. The WebSocket API was introduced as experimental in 7.86.0 and is still
  72. experimental today.
  73. It is only built\-in if explicitly opted in at build time. We discourage use of
  74. the WebSocket API in production because of its experimental state. We might
  75. change API, ABI and behavior before this "goes live".
  76. .SH SEE ALSO
  77. .BR CURLOPT_CONNECT_ONLY (3),
  78. .BR CURLOPT_WRITEFUNCTION (3),
  79. .BR CURLOPT_WS_OPTIONS (3),
  80. .BR curl_easy_init (3),
  81. .BR curl_ws_meta (3),
  82. .BR curl_ws_recv (3),
  83. .BR curl_ws_send (3)