Skip to content

Commit 3447e55

Browse files
committed
docs: add "How to deal with cookies" in FAQ
1 parent 5f5b396 commit 3447e55

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/site/markdown/faq.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Frequently asked questions
2+
3+
<!-- MACRO{toc} -->
4+
5+
## How to deal with cookies
6+
7+
In order to store the cookies sent by the server and include them in all subsequent requests, you need to create an OkHttpClient with a custom [cookie jar](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cookie-jar/).
8+
9+
You can either implement your own cookie jar:
10+
11+
```java
12+
public class MyApp {
13+
14+
public static void main(String[] argz) throws Exception {
15+
IO.Options options = new IO.Options();
16+
17+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
18+
.cookieJar(new MyCookieJar())
19+
.build();
20+
21+
options.callFactory = okHttpClient;
22+
options.webSocketFactory = okHttpClient;
23+
24+
Socket socket = IO.socket(URI.create("https://example.com"), options);
25+
26+
socket.connect();
27+
}
28+
29+
private static class MyCookieJar implements CookieJar {
30+
private Set<WrappedCookie> cache = new HashSet<>();
31+
32+
@Override
33+
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
34+
for (Cookie cookie : cookies) {
35+
this.cache.add(new WrappedCookie(cookie));
36+
}
37+
}
38+
39+
@Override
40+
public List<Cookie> loadForRequest(HttpUrl url) {
41+
List<Cookie> cookies = new ArrayList<>();
42+
Iterator<WrappedCookie> iterator = this.cache.iterator();
43+
while (iterator.hasNext()) {
44+
Cookie cookie = iterator.next().cookie;
45+
if (isCookieExpired(cookie)) {
46+
iterator.remove();
47+
} else if (cookie.matches(url)) {
48+
cookies.add(cookie);
49+
}
50+
}
51+
return cookies;
52+
}
53+
54+
private static boolean isCookieExpired(Cookie cookie) {
55+
return cookie.expiresAt() < System.currentTimeMillis();
56+
}
57+
}
58+
59+
private static class WrappedCookie {
60+
private final Cookie cookie;
61+
62+
public WrappedCookie(Cookie cookie) {
63+
this.cookie = cookie;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (!(o instanceof WrappedCookie)) return false;
69+
WrappedCookie that = (WrappedCookie) o;
70+
return that.cookie.name().equals(this.cookie.name())
71+
&& that.cookie.domain().equals(this.cookie.domain())
72+
&& that.cookie.path().equals(this.cookie.path())
73+
&& that.cookie.secure() == this.cookie.secure()
74+
&& that.cookie.hostOnly() == this.cookie.hostOnly();
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
int hash = 17;
80+
hash = 31 * hash + cookie.name().hashCode();
81+
hash = 31 * hash + cookie.domain().hashCode();
82+
hash = 31 * hash + cookie.path().hashCode();
83+
hash = 31 * hash + (cookie.secure() ? 0 : 1);
84+
hash = 31 * hash + (cookie.hostOnly() ? 0 : 1);
85+
return hash;
86+
}
87+
}
88+
}
89+
```
90+
91+
Or use a package like [PersistentCookieJar](https://github.com/franmontiel/PersistentCookieJar):
92+
93+
```java
94+
public class MyApp {
95+
96+
public static void main(String[] argz) throws Exception {
97+
IO.Options options = new IO.Options();
98+
99+
ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
100+
101+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
102+
.cookieJar(cookieJar)
103+
.build();
104+
105+
options.callFactory = okHttpClient;
106+
options.webSocketFactory = okHttpClient;
107+
108+
Socket socket = IO.socket(URI.create("https://example.com"), options);
109+
110+
socket.connect();
111+
}
112+
}
113+
```
114+
115+
## How to use with AWS Load Balancing
116+
117+
When scaling to multiple Socket.IO servers, you must ensure that all the HTTP requests of a given session reach the same server (explanation [here](https://socket.io/docs/v4/using-multiple-nodes/#why-is-sticky-session-required)).
118+
119+
Sticky sessions can be enabled on AWS Application Load Balancers, which works by sending a cookie (`AWSALB`) to the client.
120+
121+
Please see [above](#how-to-deal-with-cookies) for how to deal with cookies.
122+
123+
Reference: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html

src/site/site.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<item name="Listening to events" href="./listening_to_events.html"/>
3131
<item name="The Socket instance" href="./socket_instance.html"/>
3232
<item name="Migrating from 1.x" href="./migrating_from_1_x.html"/>
33+
<item name="FAQ" href="./faq.html"/>
3334
</menu>
3435

3536
<menu name="Miscellaneous">

0 commit comments

Comments
 (0)