aboutsummaryrefslogtreecommitdiffstats
path: root/manual/gun.md
blob: 3baa1f78b12cde8930cb607d8b158d1f72636ccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
gun
===

The `gun` module provides an asynchronous interface for
connecting and communicating with Web servers over SPDY,
HTTP or Websocket.

Types
-----

### opts() = [{keepalive, pos_integer()}
	| {retry, non_neg_integer()}
	| {retry_timeout, pos_integer()}
	| {type, ssl | tcp | tcp_spdy}].

> Configuration for the connection.

Option descriptions
-------------------

The default value is given next to the option name.

 -  keepalive (5000)
   -  Time between pings in milliseconds.
 -  retry (5)
   -  Number of times Gun will try to reconnect on failure before giving up.
 -  retry_timeout (5000)
   -  Time between retries in milliseconds.
 -  type (ssl)
   -  Whether to use SSL, plain TCP (for HTTP/Websocket) or SPDY over TCP.

Messages
--------

Calling functions from this module may result in the following
messages being sent.

### {gun_push, ServerPid, StreamRef, AssocToStreamRef,
	Method, Host, Path, Headers}

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = AssocToStreamRef = reference()
>  *  Method = binary()
>  *  Host = binary()
>  *  Path = binary()
>  *  Headers = [{binary(), binary()}]
>
> A resource pushed alongside an HTTP response.

### {gun_response, ServerPid, StreamRef, IsFin, Status, Headers}

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = reference()
>  *  IsFin = fin | nofin
>  *  Status = binary()
>  *  Headers = [{binary(), binary()}]
>
> A response to an HTTP request.

### {gun_data, ServerPid, StreamRef, IsFin, Data}

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = reference()
>  *  IsFin = fin | nofin
>  *  Data = binary()
>
> Data associated with a response or pushed resource.

### {gun_error, ServerPid, StreamRef, Reason}

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = reference()
>  *  Reason = any()
>
> An error specific to a particular stream.

### {gun_error, ServerPid, Reason}

> Types:
>  *  ServerPid = pid()
>  *  Reason = any()
>
> A general error.

### {gun_ws_upgrade, ServerPid, ok}

> Types:
>  *  ServerPid = pid()
>
> Websocket upgrade success.

### {gun_ws_upgrade, ServerPid, error, IsFin, Status, Headers}

> Types:
>  *  ServerPid = pid()
>  *  IsFin = fin | nofin
>  *  Status = binary()
>  *  Headers = [{binary(), binary()}]
>
> Websocket upgrade failure, with the HTTP response received.

### {gun_ws, ServerPid, Frame}

> Types:
>  *  ServerPid = pid()
>  *  Frame = ws_frame()
>
> A Websocket frame just received.

Exports
-------

### open(Host, Port) -> open(Host, Port, [])
### open(Host, Port, Opts) -> {ok, ServerPid} | {error, any()}

> Types:
>  *  Host = inet:hostname()
>  *  Port = inet:port_number()
>  *  Opts = opts()
>  *  ServerPid = pid()
>
> Open a connection to the given host.

### close(ServerPid) -> ok

> Types:
>  *  ServerPid = pid()
>
> Brutally close the connection.

### shutdown(ServerPid) -> ok

> Types:
>  *  ServerPid = pid()
>
> Gracefully close the connection.
>
> A monitor can be used to be notified when the connection is
> effectively closed.

### delete(ServerPid, Path) -> delete(ServerPid, Path, [])
### delete(ServerPid, Path, Headers) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>
> Delete a resource.

### get(ServerPid, Path) -> delete(ServerPid, Path, [])
### get(ServerPid, Path, Headers) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>
> Fetch a resource.

### head(ServerPid, Path) -> delete(ServerPid, Path, [])
### head(ServerPid, Path, Headers) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>
> Fetch a resource's headers.
>
> The server will not send the resource content, only headers.

### options(ServerPid, Path) -> delete(ServerPid, Path, [])
### options(ServerPid, Path, Headers) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>
> Obtain information about the capabilities of the server or a resource.
>
> The special path "*" can be used to obtain information about
> the server as a whole. Any other path will return information
> about the resource only.

### patch(ServerPid, Path, Headers) -> StreamRef
### patch(ServerPid, Path, Headers, Body) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>  *  Body = iodata()
>
> Partially update a resource.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.

### post(ServerPid, Path, Headers) -> StreamRef
### post(ServerPid, Path, Headers, Body) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>  *  Body = iodata()
>
> Create or update a resource.
>
> The resource may be created at a different URL than the one
> given.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.

### put(ServerPid, Path, Headers) -> StreamRef
### put(ServerPid, Path, Headers, Body) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>  *  Body = iodata()
>
> Create or update a resource.
>
> The resource will be created at this exact URL.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.

### request(ServerPid, Method, Path, Headers) -> StreamRef
### request(ServerPid, Method, Path, Headers, Body) -> StreamRef

> Types:
>  *  ServerPid = pid()
>  *  Method = iodata()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>  *  StreamRef = reference()
>  *  Body = iodata()
>
> Perform the given request on a resource.
>
> This is a general purpose function that should only be used
> when no other function can be used.
>
> Method names are case sensitive.
>
> Always set the content-type header so that Gun and the server
> can be made aware that a body is going to be sent. Also try
> to set the content-length header when possible.
>
> If a body is given, even an empty one, it is expected to be
> the full resource.
>
> If not, Gun will assume there is no body if content-type
> isn't set, and otherwise will expect you to stream the body.

### data(ServerPid, StreamRef, IsFin, Data) -> ok

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = reference()
>  *  IsFin = fin | nofin
>  *  Data = iodata()
>
> Stream data.
>
> The `StreamRef` argument is the one returned by any of the
> request functions beforehand and uniquely identifies a request.
>
> Use `nofin` for all chunks except the last which should be `fin`.
> The last chunk may be empty.

### cancel(ServerPid, StreamRef) -> ok

> Types:
>  *  ServerPid = pid()
>  *  StreamRef = reference()
>
> Cancel the given stream.
>
> The `StreamRef` argument is the one returned by any of the
> request functions beforehand and uniquely identifies a request.
>
> This function will do a best effort at canceling the stream,
> depending on the capabilities of the protocol.

### ws_upgrade(ServerPid, Path) -> ws_upgrade(ServerPid, Path, [])
### ws_upgrade(ServerPid, Path, Headers) -> ok

> Types:
>  *  ServerPid = pid()
>  *  Path = iodata()
>  *  Headers = [{binary(), iodata()}]
>
> Upgrade the connection to Websocket.

### ws_send(ServerPid, Frames) -> ok

> Types:
>  *  ServerPid = pid()
>  *  Frames = ws_frame() | [ws_frame()]
>
> Send a Websocket frame.