xref: /aosp_15_r20/external/curl/docs/libcurl/libcurl-ws.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: libcurl-ws
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_CONNECT_ONLY (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_WRITEFUNCTION (3)
10*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_WS_OPTIONS (3)
11*6236dae4SAndroid Build Coastguard Worker  - curl_easy_init (3)
12*6236dae4SAndroid Build Coastguard Worker  - curl_ws_meta (3)
13*6236dae4SAndroid Build Coastguard Worker  - curl_ws_recv (3)
14*6236dae4SAndroid Build Coastguard Worker  - curl_ws_send (3)
15*6236dae4SAndroid Build Coastguard WorkerProtocol:
16*6236dae4SAndroid Build Coastguard Worker  - All
17*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.86.0
18*6236dae4SAndroid Build Coastguard Worker---
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard Worker# NAME
21*6236dae4SAndroid Build Coastguard Worker
22*6236dae4SAndroid Build Coastguard Workerlibcurl-ws - WebSocket interface overview
23*6236dae4SAndroid Build Coastguard Worker
24*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
25*6236dae4SAndroid Build Coastguard Worker
26*6236dae4SAndroid Build Coastguard WorkerThe WebSocket interface provides functions for receiving and sending WebSocket
27*6236dae4SAndroid Build Coastguard Workerdata.
28*6236dae4SAndroid Build Coastguard Worker
29*6236dae4SAndroid Build Coastguard Worker# INCLUDE
30*6236dae4SAndroid Build Coastguard Worker
31*6236dae4SAndroid Build Coastguard WorkerYou still only include \<curl/curl.h\> in your code.
32*6236dae4SAndroid Build Coastguard Worker
33*6236dae4SAndroid Build Coastguard Worker# SETUP
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard WorkerWebSocket is also often known as *WebSockets*, in plural. It is done by
36*6236dae4SAndroid Build Coastguard Workerupgrading a regular HTTP(S) GET request to a WebSocket connection.
37*6236dae4SAndroid Build Coastguard Worker
38*6236dae4SAndroid Build Coastguard WorkerWebSocket is a TCP-like message-based communication protocol done over HTTP,
39*6236dae4SAndroid Build Coastguard Workerspecified in RFC 6455.
40*6236dae4SAndroid Build Coastguard Worker
41*6236dae4SAndroid Build Coastguard WorkerTo initiate a WebSocket session with libcurl, setup an easy handle to use a
42*6236dae4SAndroid Build Coastguard WorkerURL with a "WS://" or "WSS://" scheme. "WS" is for cleartext communication
43*6236dae4SAndroid Build Coastguard Workerover HTTP and "WSS" is for doing WebSocket securely over HTTPS.
44*6236dae4SAndroid Build Coastguard Worker
45*6236dae4SAndroid Build Coastguard WorkerA WebSocket request is done as an HTTP/1 GET request with an "Upgrade
46*6236dae4SAndroid Build Coastguard WorkerWebSocket" request header field. When the upgrade is accepted by the server,
47*6236dae4SAndroid Build Coastguard Workerit responds with a 101 Switching and then the client can speak WebSocket with
48*6236dae4SAndroid Build Coastguard Workerthe server. The communication can happen in both directions at the same time.
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard Worker# MESSAGES
51*6236dae4SAndroid Build Coastguard Worker
52*6236dae4SAndroid Build Coastguard WorkerWebSocket communication is message based. That means that both ends send and
53*6236dae4SAndroid Build Coastguard Workerreceive entire messages, not streams like TCP. A WebSocket message is sent
54*6236dae4SAndroid Build Coastguard Workerover the wire in one or more frames. Each frame in a message can have a size
55*6236dae4SAndroid Build Coastguard Workerup to 2^63 bytes.
56*6236dae4SAndroid Build Coastguard Worker
57*6236dae4SAndroid Build Coastguard Workerlibcurl delivers WebSocket data as frame fragments. It might send a whole
58*6236dae4SAndroid Build Coastguard Workerframe, but it might also deliver them in pieces depending on size and network
59*6236dae4SAndroid Build Coastguard Workerpatterns. It makes sure to provide the API user about the exact specifics
60*6236dae4SAndroid Build Coastguard Workerabout the fragment: type, offset, size and how much data there is pending to
61*6236dae4SAndroid Build Coastguard Workerarrive for the same frame.
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerA message has an unknown size until the last frame header for the message has
64*6236dae4SAndroid Build Coastguard Workerbeen received since only frames have set sizes.
65*6236dae4SAndroid Build Coastguard Worker
66*6236dae4SAndroid Build Coastguard Worker# Raw mode
67*6236dae4SAndroid Build Coastguard Worker
68*6236dae4SAndroid Build Coastguard Workerlibcurl can be told to speak WebSocket in "raw mode" by setting the
69*6236dae4SAndroid Build Coastguard Worker**CURLWS_RAW_MODE** bit to the CURLOPT_WS_OPTIONS(3) option.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerRaw WebSocket means that libcurl passes on the data from the network without
72*6236dae4SAndroid Build Coastguard Workerparsing it leaving that entirely to the application. This mode assumes that
73*6236dae4SAndroid Build Coastguard Workerthe user of this knows WebSocket and can parse and figure out the data all by
74*6236dae4SAndroid Build Coastguard Workeritself.
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard WorkerThis mode is intended for applications that already have a WebSocket
77*6236dae4SAndroid Build Coastguard Workerparser/engine that want to switch over to use libcurl for enabling WebSocket,
78*6236dae4SAndroid Build Coastguard Workerand keep parts of the existing software architecture.
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard Worker# PING
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard WorkerWebSocket is designed to allow long-lived sessions and in order to keep the
83*6236dae4SAndroid Build Coastguard Workerconnections alive, both ends can send PING messages for the other end to
84*6236dae4SAndroid Build Coastguard Workerrespond with a PONG.
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard Workerlibcurl automatically responds to server PING messages with a PONG. It does
87*6236dae4SAndroid Build Coastguard Workernot send any PING messages automatically.
88*6236dae4SAndroid Build Coastguard Worker
89*6236dae4SAndroid Build Coastguard Worker# MODELS
90*6236dae4SAndroid Build Coastguard Worker
91*6236dae4SAndroid Build Coastguard WorkerBecause of the many different ways WebSocket can be used, which is much more
92*6236dae4SAndroid Build Coastguard Workerflexible than limited to plain downloads or uploads, libcurl offers two
93*6236dae4SAndroid Build Coastguard Workerdifferent API models to use it:
94*6236dae4SAndroid Build Coastguard Worker
95*6236dae4SAndroid Build Coastguard Worker1. Using a write callback with CURLOPT_WRITEFUNCTION(3) much like other
96*6236dae4SAndroid Build Coastguard Workerdownloads for when the traffic is download oriented.
97*6236dae4SAndroid Build Coastguard Worker
98*6236dae4SAndroid Build Coastguard Worker2. Using CURLOPT_CONNECT_ONLY(3) and use the WebSocket recv/send
99*6236dae4SAndroid Build Coastguard Workerfunctions.
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard Worker# Callback model
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard WorkerWhen a write callback is set and a WebSocket transfer is performed, the
104*6236dae4SAndroid Build Coastguard Workercallback is called to deliver all WebSocket data that arrives.
105*6236dae4SAndroid Build Coastguard Worker
106*6236dae4SAndroid Build Coastguard WorkerThe callback can then call curl_ws_meta(3) to learn about the details of
107*6236dae4SAndroid Build Coastguard Workerthe incoming data fragment.
108*6236dae4SAndroid Build Coastguard Worker
109*6236dae4SAndroid Build Coastguard Worker# CONNECT_ONLY model
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard WorkerBy setting CURLOPT_CONNECT_ONLY(3) to **2L**, the transfer only
112*6236dae4SAndroid Build Coastguard Workerestablishes and setups the WebSocket communication and then returns control
113*6236dae4SAndroid Build Coastguard Workerback to the application.
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard WorkerOnce such a setup has been successfully performed, the application can proceed
116*6236dae4SAndroid Build Coastguard Workerand use curl_ws_recv(3) and curl_ws_send(3) freely to exchange
117*6236dae4SAndroid Build Coastguard WorkerWebSocket messages with the server.
118*6236dae4SAndroid Build Coastguard Worker
119*6236dae4SAndroid Build Coastguard Worker# EXPERIMENTAL
120*6236dae4SAndroid Build Coastguard Worker
121*6236dae4SAndroid Build Coastguard WorkerThe WebSocket API was introduced as experimental in 7.86.0 and is still
122*6236dae4SAndroid Build Coastguard Workerexperimental today.
123*6236dae4SAndroid Build Coastguard Worker
124*6236dae4SAndroid Build Coastguard WorkerIt is only built-in if explicitly opted in at build time. We discourage use of
125*6236dae4SAndroid Build Coastguard Workerthe WebSocket API in production because of its experimental state. We might
126*6236dae4SAndroid Build Coastguard Workerchange API, ABI and behavior before this "goes live".
127