Skip to content

Commit 9756d6d

Browse files
authored
Update Websocket Example (#25970)
This updates the example in several ways: - Send back the number of live connections. The docs say this WS server will send back the number of live connections and now it does. - Removing the confusing `k` lambda parameter. The `k` in this case is unused and _not_ a key. - Update the message contents so they are not the same in all cases.
1 parent 9932313 commit 9756d6d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/content/docs/durable-objects/examples/websocket-server.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ export class WebSocketServer extends DurableObject {
108108
const connection = this.sessions.get(ws)!;
109109

110110
// Reply back with the same message to the connection
111-
ws.send(`[Durable Object] message: ${message}, from: ${connection.id}`);
111+
ws.send(`[Durable Object] message: ${message}, from: ${connection.id}, to: the initiating client. Total connections: ${this.sessions.size}`);
112112

113113
// Broadcast the message to all the connections,
114114
// except the one that sent the message.
115-
this.sessions.forEach((k, session) => {
115+
this.sessions.forEach((_, session) => {
116116
if (session !== ws) {
117-
session.send(`[Durable Object] message: ${message}, from: ${connection.id}`);
117+
session.send(`[Durable Object] message: ${message}, from: ${connection.id}, to: all clients except the initiating client. Total connections: ${this.sessions.size}`);
118118
}
119119
});
120120

121121
// Broadcast the message to all the connections,
122122
// including the one that sent the message.
123-
this.sessions.forEach((k, session) => {
124-
session.send(`[Durable Object] message: ${message}, from: ${connection.id}`);
123+
this.sessions.forEach((_, session) => {
124+
session.send(`[Durable Object] message: ${message}, from: ${connection.id}, to: all clients. Total connections: ${this.sessions.size}`);
125125
});
126126
}
127127

0 commit comments

Comments
 (0)