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
|
---
name: Set per-socket contextual data on a WebSocket
---
When building a WebSocket server, it's typically necessary to store some identifying information or context associated with each connected client.
With [Bun.serve()](/docs/api/websockets#contextual-data), this "contextual data" is set when the connection is initially upgraded by passing a `data` parameter in the `server.upgrade()` call.
```ts
Bun.serve<{ socketId: number }>({
fetch(req, server) {
const success = server.upgrade(req, {
data: {
socketId: Math.random(),
},
});
if (success) return undefined;
// handle HTTP request normally
// ...
},
websocket: {
// define websocket handlers
async message(ws, message) {
// the contextual dta is available as the `data` property
// on the WebSocket instance
console.log(`Received ${message} from ${ws.data.socketId}}`);
},
},
});
```
---
It's common to read cookies/headers from the incoming request to identify the connecting client.
```ts
type WebSocketData = {
createdAt: number;
token: string;
userId: string;
};
// TypeScript: specify the type of `data`
Bun.serve<WebSocketData>({
async fetch(req, server) {
// use a library to parse cookies
const cookies = parseCookies(req.headers.get("Cookie"));
const token = cookies["X-Token"];
const user = await getUserFromToken(ws.data.authToken);
const upgraded = server.upgrade(req, {
data: {
createdAt: Date.now(),
token: cookies["X-Token"],
userId: user.id,
},
});
if (upgraded) return undefined;
},
websocket: {
async message(ws, message) {
// save the message to a database
await saveMessageToDatabase({
message: String(message),
userId: ws.data.userId,
});
},
},
});
```
|