@zero-server/realtime 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ws/room.js ADDED
@@ -0,0 +1,223 @@
1
+ /**
2
+ * @module ws/room
3
+ * @description WebSocket room/channel manager.
4
+ * Provides broadcast, room-based messaging, and connection
5
+ * registry for WebSocket connections.
6
+ */
7
+
8
+ /**
9
+ * Manages a pool of WebSocket connections with room-based grouping.
10
+ *
11
+ * @example
12
+ * const pool = new WebSocketPool();
13
+ * app.ws('/chat', (ws, req) => {
14
+ * pool.add(ws);
15
+ * pool.join(ws, 'general');
16
+ * ws.on('message', msg => pool.toRoom('general', msg));
17
+ * ws.on('close', () => pool.remove(ws));
18
+ * });
19
+ */
20
+ class WebSocketPool
21
+ {
22
+ /** @constructor */
23
+ constructor()
24
+ {
25
+ /** @type {Set<import('./connection')>} All active connections. */
26
+ this._connections = new Set();
27
+ /** @type {Map<string, Set<import('./connection')>>} Room → connection sets. */
28
+ this._rooms = new Map();
29
+ }
30
+
31
+ /**
32
+ * Add a connection to the pool.
33
+ * @param {import('./connection')} ws - WebSocket connection.
34
+ * @returns {WebSocketPool} this
35
+ */
36
+ add(ws)
37
+ {
38
+ this._connections.add(ws);
39
+
40
+ // Auto-remove on close
41
+ ws.once('close', () => this.remove(ws));
42
+
43
+ return this;
44
+ }
45
+
46
+ /**
47
+ * Remove a connection from the pool and all rooms.
48
+ * @param {import('./connection')} ws - WebSocket connection.
49
+ * @returns {WebSocketPool} this
50
+ */
51
+ remove(ws)
52
+ {
53
+ this._connections.delete(ws);
54
+ for (const [room, members] of this._rooms)
55
+ {
56
+ members.delete(ws);
57
+ if (members.size === 0) this._rooms.delete(room);
58
+ }
59
+ return this;
60
+ }
61
+
62
+ /**
63
+ * Join a connection to a room.
64
+ * @param {import('./connection')} ws - WebSocket connection.
65
+ * @param {string} room - Room name.
66
+ * @returns {WebSocketPool} this
67
+ */
68
+ join(ws, room)
69
+ {
70
+ if (!this._rooms.has(room)) this._rooms.set(room, new Set());
71
+ this._rooms.get(room).add(ws);
72
+ return this;
73
+ }
74
+
75
+ /**
76
+ * Remove a connection from a room.
77
+ * @param {import('./connection')} ws - WebSocket connection.
78
+ * @param {string} room - Room name.
79
+ * @returns {WebSocketPool} this
80
+ */
81
+ leave(ws, room)
82
+ {
83
+ const members = this._rooms.get(room);
84
+ if (members)
85
+ {
86
+ members.delete(ws);
87
+ if (members.size === 0) this._rooms.delete(room);
88
+ }
89
+ return this;
90
+ }
91
+
92
+ /**
93
+ * Get all rooms a connection belongs to.
94
+ * @param {import('./connection')} ws - WebSocket connection.
95
+ * @returns {string[]} Room names the connection belongs to.
96
+ */
97
+ roomsOf(ws)
98
+ {
99
+ const result = [];
100
+ for (const [room, members] of this._rooms)
101
+ {
102
+ if (members.has(ws)) result.push(room);
103
+ }
104
+ return result;
105
+ }
106
+
107
+ /**
108
+ * Broadcast a message to ALL connected clients.
109
+ * @param {string|Buffer} data - Payload.
110
+ * @param {import('./connection')} [exclude] - Optional connection to exclude (e.g. the sender).
111
+ */
112
+ broadcast(data, exclude)
113
+ {
114
+ for (const ws of this._connections)
115
+ {
116
+ if (ws !== exclude && ws.readyState === 1) ws.send(data);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Broadcast a JSON message to ALL connected clients.
122
+ * @param {*} obj - Value to serialise.
123
+ * @param {import('./connection')} [exclude] - Connection(s) to exclude.
124
+ */
125
+ broadcastJSON(obj, exclude)
126
+ {
127
+ const msg = JSON.stringify(obj);
128
+ this.broadcast(msg, exclude);
129
+ }
130
+
131
+ /**
132
+ * Send a message to all connections in a specific room.
133
+ * @param {string} room - Room name.
134
+ * @param {string|Buffer} data - Payload.
135
+ * @param {import('./connection')} [exclude] - Connection(s) to exclude.
136
+ */
137
+ toRoom(room, data, exclude)
138
+ {
139
+ const members = this._rooms.get(room);
140
+ if (!members) return;
141
+ for (const ws of members)
142
+ {
143
+ if (ws !== exclude && ws.readyState === 1) ws.send(data);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Send a JSON message to all connections in a specific room.
149
+ * @param {string} room - Room name.
150
+ * @param {*} obj - Data object to send.
151
+ * @param {import('./connection')} [exclude] - Connection(s) to exclude.
152
+ */
153
+ toRoomJSON(room, obj, exclude)
154
+ {
155
+ this.toRoom(room, JSON.stringify(obj), exclude);
156
+ }
157
+
158
+ /**
159
+ * Get all connections in a room.
160
+ * @param {string} room - Room name.
161
+ * @returns {import('./connection')[]} Connections in the room (empty array if the room does not exist).
162
+ */
163
+ in(room)
164
+ {
165
+ const members = this._rooms.get(room);
166
+ return members ? Array.from(members) : [];
167
+ }
168
+
169
+ /**
170
+ * Total number of active connections.
171
+ * @type {number}
172
+ */
173
+ get size()
174
+ {
175
+ return this._connections.size;
176
+ }
177
+
178
+ /**
179
+ * Number of connections in a specific room.
180
+ * @param {string} room - Room name.
181
+ * @returns {number} Number of connections in the room.
182
+ */
183
+ roomSize(room)
184
+ {
185
+ const members = this._rooms.get(room);
186
+ return members ? members.size : 0;
187
+ }
188
+
189
+ /**
190
+ * List all active room names.
191
+ * @returns {string[]} Array of room names.
192
+ */
193
+ get rooms()
194
+ {
195
+ return Array.from(this._rooms.keys());
196
+ }
197
+
198
+ /**
199
+ * Get all active connections.
200
+ * @returns {import('./connection')[]} Array of connections in the pool.
201
+ */
202
+ get clients()
203
+ {
204
+ return Array.from(this._connections);
205
+ }
206
+
207
+ /**
208
+ * Close all connections gracefully.
209
+ * @param {number} [code=1001] - Close code.
210
+ * @param {string} [reason] - Close reason.
211
+ */
212
+ closeAll(code = 1001, reason = 'Server shutdown')
213
+ {
214
+ for (const ws of this._connections)
215
+ {
216
+ ws.close(code, reason);
217
+ }
218
+ this._connections.clear();
219
+ this._rooms.clear();
220
+ }
221
+ }
222
+
223
+ module.exports = WebSocketPool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zero-server/realtime",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "WebSocket connection + room manager and SSE stream controller.",
5
5
  "keywords": [
6
6
  "zero-server",
@@ -20,6 +20,7 @@
20
20
  "./package.json": "./package.json"
21
21
  },
22
22
  "files": [
23
+ "lib",
23
24
  "index.js",
24
25
  "index.d.ts",
25
26
  "README.md",
@@ -42,7 +43,12 @@
42
43
  "access": "public"
43
44
  },
44
45
  "sideEffects": false,
45
- "dependencies": {
46
- "@zero-server/sdk": "0.9.1"
46
+ "peerDependencies": {
47
+ "@zero-server/sdk": ">=0.9.2"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "@zero-server/sdk": {
51
+ "optional": true
52
+ }
47
53
  }
48
54
  }