@eleven-am/pondsocket 0.1.141 → 0.1.143

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/types.d.ts CHANGED
@@ -1,16 +1,17 @@
1
1
  import { Server as HTTPServer } from 'http';
2
2
 
3
3
  import {
4
- PondMessage,
5
- PondPresence,
4
+ IncomingConnection,
5
+ JoinParams,
6
6
  PondAssigns,
7
7
  PondEvent,
8
- PondPath,
9
- JoinParams,
10
8
  PondEventMap,
11
- IncomingConnection, UserData,
9
+ PondMessage,
10
+ PondPath,
11
+ PondPresence,
12
+ UserData,
12
13
  } from '@eleven-am/pondsocket-common';
13
- import { WebSocketServer } from 'ws';
14
+ import { WebSocket, WebSocketServer } from 'ws';
14
15
 
15
16
  export * from '@eleven-am/pondsocket-common';
16
17
 
@@ -21,143 +22,126 @@ export interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, Pres
21
22
 
22
23
  export type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
23
24
 
24
- export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
25
- event: PondEvent<Path>;
26
-
27
- channelName: string;
28
-
29
- assigns: Record<string, AssignType>;
30
-
31
- presence: Record<string, PresenceType>;
32
- }
33
-
34
- export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
35
- user: UserData<PresenceType, AssignType>;
36
-
37
- channel: Channel;
38
- }
39
-
40
- export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
41
- /**
42
- * @desc Whether the server has responded to the request
43
- */
44
- hasResponded: boolean;
45
-
46
- /**
47
- * @desc Accepts the request and optionally assigns data to the client
48
- * @param assigns - the data to assign to the client
49
- */
50
- accept (assigns?: AssignType): EventResponse;
51
-
52
- /**
53
- * @desc Rejects the request and optionally assigns data to the client
54
- * @param message - the error message
55
- * @param errorCode - the error code
56
- * @param assigns - the data to assign to the client
57
- */
58
- reject (message?: string, errorCode?: number, assigns?: AssignType): EventResponse;
25
+ export type RequestHandler<Request, Response> = (request: Request, response: Response) => void | Promise<void>;
59
26
 
60
- /**
61
- * @desc Emits a direct message to the client, accepting the request
62
- * @param event - the event name
63
- * @param payload - the payload to send
64
- * @param assigns - the data to assign to the client
65
- */
66
- send (event: string, payload: PondMessage, assigns?: AssignType): void;
27
+ export declare class PondSocket {
28
+ constructor(server?: HTTPServer, socketServer?: WebSocketServer);
67
29
 
68
30
  /**
69
- * @desc Emits a direct message to the client without accepting the request
70
- * @param event - the event name
71
- * @param payload - the payload to send
72
- * @param assigns - the data to assign to the client
31
+ * @desc Specifies the port to listen on
32
+ * @param args - the arguments to pass to the server
73
33
  */
74
- sendOnly <Key extends keyof EventType> (event: Key, payload: EventType[Key], assigns?: AssignType): void;
34
+ listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
75
35
 
76
36
  /**
77
- * @desc Sends a message to all clients in the channel
78
- * @param event - the event to send
79
- * @param payload - the payload to send
37
+ * @desc Closes the server
38
+ * @param callback - the callback to call when the server is closed
80
39
  */
81
- broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): EventResponse;
40
+ close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
82
41
 
83
42
  /**
84
- * @desc Sends a message to all clients in the channel except the client making the request
85
- * @param event - the event to send
86
- * @param payload - the payload to send
43
+ * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
44
+ * @param path - the pattern to accept || can also be a regex
45
+ * @param handler - the handler function to authenticate the socket
46
+ * @example
47
+ * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
48
+ * const token = req.query.token;
49
+ * if (!token)
50
+ * return res.reject('No token provided');
51
+ * res.accept({
52
+ * assign: {
53
+ * token
54
+ * }
55
+ * });
56
+ * })
87
57
  */
88
- broadcastFromUser <Key extends keyof EventType> (event: Key, payload: EventType[Key]): EventResponse;
58
+ createEndpoint<Path extends string>(path: PondPath<Path>, handler: RequestHandler<IncomingConnection<Path>, ConnectionResponse>): Endpoint;
59
+ }
89
60
 
61
+ export declare class Endpoint {
90
62
  /**
91
- * @desc Sends a message to a set of clients in the channel
92
- * @param event - the event to send
93
- * @param payload - the payload to send
94
- * @param userIds - the ids of the clients to send the message to
63
+ * @desc Adds a new PondChannel to this path on this endpoint
64
+ * @param {PondPath<string>} path - The path to add the channel to
65
+ * @param {RequestHandler} handler - The handler to use to authenticate the client
66
+ * @returns {PondChannel} - The PondChannel instance for the new channel
67
+ *
68
+ * @example
69
+ * const channel = endpoint.createChannel('/chat', (request, response) => {
70
+ * if (request.user.assigns.admin)
71
+ * response.accept();
72
+ *
73
+ * else
74
+ * response.reject('You are not an admin', 403);
75
+ * });
95
76
  */
96
- sendToUsers <Key extends keyof EventType> (event: Key, payload: EventType[Key], userIds: string[]): EventResponse;
77
+ createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: RequestHandler<JoinRequest<Path, PresenceType, AssignType>, JoinResponse<EventType, PresenceType, AssignType>>): PondChannel<EventType, PresenceType, AssignType>;
97
78
 
98
79
  /**
99
- * @desc Tracks a user's presence in the channel
100
- * @param presence - the initial presence data
101
- * @param userId - the id of the user to track
80
+ * @desc Broadcasts a message to all clients connected to this endpoint
81
+ * @param {string} event - The event to broadcast.
82
+ * @param {PondMessage} payload - The payload to broadcast.
83
+ * @returns {void}
102
84
  */
103
- trackPresence (presence: PresenceType, userId?: string): EventResponse;
85
+ broadcast(event: string, payload: PondMessage): void;
104
86
 
105
87
  /**
106
- * @desc Updates a user's presence in the channel
107
- * @param presence - the updated presence data
108
- * @param userId - the id of the user to update
88
+ * @desc Closes specific clients connected to this endpoint
89
+ * @param {string | string[]} clientIds - The ids of the clients to close.
109
90
  */
110
- updatePresence (presence: PresenceType, userId?: string): EventResponse;
91
+ closeConnection(clientIds: string | string[]): void;
111
92
 
112
93
  /**
113
- * @desc Removes a user's presence from the channel
114
- * @param userId - the id of the user to remove
94
+ * @desc Returns all the clients connected to this endpoint
115
95
  */
116
- unTrackPresence (userId?: string): EventResponse;
96
+ getClients(): WebSocket[];
97
+ }
117
98
 
99
+ export declare class PondChannel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
118
100
  /**
119
- * @desc Evicts a user from the channel
120
- * @param reason - the reason for the eviction
121
- * @param userId - the id of the user to evict,
101
+ * @desc Handles an event request made by a user
102
+ * @param {PondPath<string>} event - The event to handle
103
+ * @param {RequestHandler} handler - The handlers to execute when the event is received
104
+ * @example
105
+ * pond.onEvent('echo', (request, response) => {
106
+ * response.reply('echo', {
107
+ * message: request.event.payload,
108
+ * });
109
+ * });
122
110
  */
123
- evictUser (reason: string, userId?: string): void;
111
+ onEvent<Event extends string>(event: PondPath<Event>, handler: RequestHandler<EventRequest<Event, PresenceType, AssignType>, EventResponse<EventType, PresenceType, AssignType>>): void;
124
112
 
125
113
  /**
126
- * @desc Closes the channel from the server side for all clients
127
- * @param reason - the reason for closing the channel
114
+ * @desc Broadcasts a message to all users in a channel
115
+ * @param event - The event to broadcast
116
+ * @param payload - The payload to send
117
+ * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
118
+ * @example
119
+ * pond.broadcast('echo', {
120
+ * message: 'Hello World',
121
+ * timestamp: Date.now(),
122
+ * channel: 'my_channel',
123
+ *});
128
124
  */
129
- closeChannel (reason: string): void;
130
- }
125
+ broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
131
126
 
132
- export declare class Endpoint {
133
127
  /**
134
- * @desc Adds a new PondChannel to this path on this endpoint
135
- * @param path - The path to add the channel to
136
- * @param handler - The handler to use to authenticate the client
137
- *
128
+ * @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
129
+ * @param {LeaveCallback} callback - The callback to execute when a user leaves
138
130
  * @example
139
- * const channel = endpoint.createChannel('/chat', (request, response) => {
140
- * if (request.user.assigns.admin)
141
- * response.accept();
142
- *
143
- * else
144
- * response.reject('You are not an admin', 403);
131
+ * pond.onLeave((event) => {
132
+ * // Clean up resources
145
133
  * });
146
134
  */
147
- createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> (path: PondPath<Path>, handler: (request: JoinRequest<Path, PresenceType, AssignType>, response: JoinResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): PondChannel<EventType, PresenceType, AssignType>;
148
-
149
- /**
150
- * @desc Broadcasts a message to all clients connected to this endpoint
151
- * @param event - The event to broadcast
152
- * @param payload - The payload to broadcast
153
- */
154
- broadcast (event: string, payload: PondMessage): void;
135
+ onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
155
136
 
156
137
  /**
157
- * @desc Closes specific clients connected to this endpoint
158
- * @param clientIds - The id for the client / clients to close
138
+ * @desc Gets a channel by name
139
+ * @param {string} channelName - The name of the channel to get
140
+ * @returns {Channel} - The channel instance
141
+ * @example
142
+ * const channel = pond.getChannel('my_channel')!;
159
143
  */
160
- closeConnection (clientIds: string | string[]): void;
144
+ getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
161
145
  }
162
146
 
163
147
  export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
@@ -169,76 +153,92 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
169
153
  /**
170
154
  * @desc Gets the current assign data for the channel.
171
155
  */
172
- getAssigns (): Record<string, AssignType>;
156
+ getAssigns(): Record<string, AssignType>;
173
157
 
174
158
  /**
175
159
  * @desc Gets the current presence data for the channel.
176
160
  */
177
- getPresences (): Record<string, PresenceType>;
161
+ getPresences(): Record<string, PresenceType>;
178
162
 
179
163
  /**
180
164
  * @desc Gets the assign date for a specific user.
181
- * @param userId - The id of the user to get the assign data for.
165
+ * @param {string} userId - The id of the user to get the data for.
182
166
  */
183
- getUserData (userId: string): AssignType | null;
167
+ getUserData(userId: string): AssignType | null;
184
168
 
185
169
  /**
186
170
  * @desc Broadcasts a message to every client in the channel,
187
- * @param event - The event to send.
188
- * @param payload - The message to send.
171
+ * @param {string} event - The event to send.
172
+ * @param {PondMessage} payload - The message to send.
189
173
  */
190
- broadcastMessage <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
174
+ broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): void;
191
175
 
192
176
  /**
193
177
  * @desc Broadcasts a message to every client in the channel except the sender,
194
- * @param userId - The id of the user to send the message from.
195
- * @param event - The event to send.
196
- * @param payload - The message to send.
178
+ * @param {string} userId - The id of the user to exclude from the broadcast.
179
+ * @param {string} event - The event to send.
180
+ * @param {PondMessage} payload - The message to send.
197
181
  */
198
- broadcastMessageFromUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
182
+ broadcastFrom<Key extends keyof EventType>(userId: string, event: Key, payload: EventType[Key]): void;
199
183
 
200
184
  /**
201
185
  * @desc Sends a message to a specific client in the channel.
202
- * @param userId - The id of the user to send the message to.
203
- * @param event - The event to send.
204
- * @param payload - The message to send.
186
+ * @param {string | string[]} clientIds - The id of the client to send the message to.
187
+ * @param {string} event - The event to send.
188
+ * @param {PondMessage} payload - The message to send.
205
189
  */
206
- sendToUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
207
-
208
- /**
209
- * @desc Sends a message to specific clients in the channel.
210
- * @param userIds - The ids of the users to send the message to.
211
- * @param event - The event to send.
212
- * @param payload - The message to send.
213
- */
214
- sendToUsers <Key extends keyof EventType> (userIds: string[], event: Key, payload: EventType[Key]): void;
190
+ broadcastTo<Key extends keyof EventType>(clientIds: string | string[], event: Key, payload: EventType[Key]): void;
215
191
 
216
192
  /**
217
193
  * @desc Bans a user from the channel.
218
- * @param userId - The id of the user to ban.
219
- * @param reason - The reason for the ban.
194
+ * @param {string} userId - The id of the user to ban.
195
+ * @param {string} reason - The reason for the ban.
220
196
  */
221
- evictUser (userId: string, reason?: string): void;
197
+ evictUser(userId: string, reason?: string): void;
222
198
 
223
199
  /**
224
200
  * @desc tracks a user's presence in the channel
225
- * @param userId - the id of the user to track
226
- * @param presence - the presence of the user
201
+ * @param {string} userId - the id of the user to track
202
+ * @param {PondPresence} presence - the presence data to track
227
203
  */
228
- trackPresence (userId: string, presence: PresenceType): void;
204
+ trackPresence(userId: string, presence: PresenceType): void;
229
205
 
230
206
  /**
231
207
  * @desc removes a user's presence from the channel
232
- * @param userId - the id of the user to remove
208
+ * @param {string} userId - the id of the user to remove
233
209
  */
234
- removePresence (userId: string): void;
210
+ removePresence(userId: string): void;
235
211
 
236
212
  /**
237
213
  * @desc updates a user's presence in the channel
238
- * @param userId - the id of the user to update
239
- * @param presence - the new presence of the user
214
+ * @param {string} userId - the id of the user to update
215
+ * @param {PondPresence} presence - the presence data to update
216
+ */
217
+ updatePresence(userId: string, presence: PresenceType): void;
218
+
219
+ /**
220
+ * @desc Subscribes a user to a channel.
221
+ * @param {string} userId - The id of the user.
222
+ * @param {string} channel - The channel to subscribe to.
240
223
  */
241
- updatePresence (userId: string, presence: PresenceType): void;
224
+ subscribeTo(userId: string, channel: string): void;
225
+
226
+ /**
227
+ * @desc Unsubscribes a user from a channel.
228
+ * @param {string} userId - The id of the user.
229
+ * @param {string} channel - The channel to unsubscribe from.
230
+ */
231
+ unsubscribeFrom(userId: string, channel: string): void;
232
+ }
233
+
234
+ export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
235
+ event: PondEvent<Path>;
236
+
237
+ channelName: string;
238
+
239
+ assigns: Record<string, AssignType>;
240
+
241
+ presence: Record<string, PresenceType>;
242
242
  }
243
243
 
244
244
  export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
@@ -249,6 +249,59 @@ export declare class JoinRequest<Path extends string, PresenceType extends PondP
249
249
  channel: Channel;
250
250
  }
251
251
 
252
+ export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
253
+ user: UserData<PresenceType, AssignType>;
254
+
255
+ channel: Channel;
256
+ }
257
+
258
+ export declare class ConnectionResponse {
259
+ /**
260
+ * @desc Whether the server has responded to the request
261
+ */
262
+ hasResponded: boolean;
263
+
264
+ /**
265
+ * @desc Assigns data to the user.
266
+ * @param {PondAssigns} assigns - The data to assign.
267
+ * @returns {ConnectionResponse} - The ConnectionResponse instance for chaining.
268
+ */
269
+ assign(assigns: PondAssigns): ConnectionResponse;
270
+
271
+ /**
272
+ * @desc Accepts the connection request to the endpoint.
273
+ */
274
+ accept(): void;
275
+
276
+ /**
277
+ * @desc Rejects the request with the given error message
278
+ * @param {string} message - The error message
279
+ * @param {number} errorCode - The error code
280
+ */
281
+ decline(message?: string, errorCode?: number): void;
282
+
283
+ /**
284
+ * @desc Emits a direct message to the client
285
+ * @param {string} event - The event name
286
+ * @param {PondMessage} payload - The payload to send
287
+ */
288
+ reply(event: string, payload: PondMessage): ConnectionResponse;
289
+
290
+ /**
291
+ * @desc Subscribes the client to a channel.
292
+ * @param {string} channel - The channel to subscribe to.
293
+ * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
294
+ */
295
+ subscribeTo(channel: string): ConnectionResponse;
296
+
297
+ /**
298
+ * @desc Unsubscribes the client from a channel.
299
+ * @param {string} channel - The channel to unsubscribe from.
300
+ * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
301
+ */
302
+ unsubscribeFrom(channel: string): ConnectionResponse;
303
+ }
304
+
252
305
  export declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
253
306
  /**
254
307
  * @desc Whether the server has responded to the request
@@ -256,154 +309,170 @@ export declare class JoinResponse<EventType extends PondEventMap = PondEventMap,
256
309
  hasResponded: boolean;
257
310
 
258
311
  /**
259
- * @desc Accepts the request and optionally assigns data to the client
260
- * @param assigns - the data to assign to the client
312
+ * @desc Assigns data to the client
313
+ * @param {PondAssigns} assigns - The data to assign
314
+ */
315
+ assign(assigns: AssignType): JoinResponse;
316
+
317
+ /**
318
+ * @desc Accepts the join request to the channel.
319
+ * @returns {JoinResponse} - The JoinResponse instance for chaining.
261
320
  */
262
- accept (assigns?: AssignType): JoinResponse;
321
+ accept(): JoinResponse;
263
322
 
264
323
  /**
265
324
  * @desc Rejects the request and optionally assigns data to the client
266
- * @param message - the error message
267
- * @param errorCode - the error code
325
+ * @param {string} message - The error message
326
+ * @param {number} errorCode - The error code
268
327
  */
269
- reject (message?: string, errorCode?: number): JoinResponse;
328
+ decline(message?: string, errorCode?: number): JoinResponse;
270
329
 
271
330
  /**
272
331
  * @desc Emits a direct message to the client
273
- * @param event - the event name
274
- * @param payload - the payload to send
275
- * @param assigns - the data to assign to the client
332
+ * @param {string} event - The event name
333
+ * @param {PondMessage} payload - The payload to send
334
+ * @param {PondAssigns} assigns - The data to assign to the client
276
335
  */
277
- send <Key extends keyof EventType> (event: Key, payload: EventType[Key], assigns?: AssignType): JoinResponse;
336
+ reply<Key extends keyof EventType>(event: Key, payload: EventType[Key], assigns?: AssignType): JoinResponse;
278
337
 
279
338
  /**
280
339
  * @desc Emits a message to all clients in the channel
281
- * @param event - the event name
282
- * @param payload - the payload to send
340
+ * @param {string} event - The event name
341
+ * @param {PondMessage} payload - The payload to send
283
342
  */
284
- broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
343
+ broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
285
344
 
286
345
  /**
287
346
  * @desc Emits a message to all clients in the channel except the sender
288
347
  * @param event - the event name
289
348
  * @param payload - the payload to send
290
349
  */
291
- broadcastFromUser <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
350
+ broadcastFrom<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinResponse;
292
351
 
293
352
  /**
294
353
  * @desc Emits a message to a specific set of clients
295
- * @param event - the event name
296
- * @param payload - the payload to send
297
- * @param userIds - the ids of the clients to send the message to
354
+ * @param {string} event - The event name
355
+ * @param {PondMessage} payload - The payload to send
356
+ * @param {string | string[]} userIds - The ids of the clients to send the message to
298
357
  */
299
- sendToUsers <Key extends keyof EventType> (event: Key, payload: EventType[Key], userIds: string[]): JoinResponse;
358
+ broadcastTo<Key extends keyof EventType>(event: Key, payload: EventType[Key], userIds: string | string[]): JoinResponse;
300
359
 
301
360
  /**
302
361
  * @desc tracks the presence of a client
303
- * @param presence - the presence data to track
362
+ * @param {PondPresence} presence - the presence data to track
304
363
  */
305
- trackPresence (presence: PresenceType): JoinResponse;
364
+ trackPresence(presence: PresenceType): JoinResponse;
365
+
366
+ /**
367
+ * @desc Subscribes the client to a channel.
368
+ * @param {string} channel - The channel to subscribe to.
369
+ * @returns {JoinResponse} - The JoinResponse instance for chaining.
370
+ */
371
+ subscribeTo(channel: string): JoinResponse;
372
+
373
+ /**
374
+ * @desc Unsubscribes the client from a channel.
375
+ * @param {string} channel - The channel to unsubscribe from.
376
+ * @returns {JoinResponse} - The JoinResponse instance for chaining.
377
+ */
378
+ unsubscribeFrom(channel: string): JoinResponse;
306
379
  }
307
380
 
308
- export declare class ConnectionResponse {
381
+ export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
309
382
  /**
310
383
  * @desc Whether the server has responded to the request
311
384
  */
312
385
  hasResponded: boolean;
313
386
 
314
387
  /**
315
- * @desc Accepts the request and optionally assigns data to the client
316
- * @param assigns - the data to assign to the client
388
+ * @desc Assigns data to the client.
389
+ * @param {PondAssigns} assigns - The data to assign to the client.
390
+ * @returns {EventResponse} - The EventResponse instance for chaining.
317
391
  */
318
- accept (assigns?: PondAssigns): void;
392
+ assign(assigns: AssignType): EventResponse<EventType, PresenceType, AssignType>;
319
393
 
320
394
  /**
321
- * @desc Rejects the request with the given error message
322
- * @param message - the error message
323
- * @param errorCode - the error code
395
+ * @desc Emits a direct message to the client.
396
+ * @param {string} event - The event name.
397
+ * @param {PondMessage} payload - The payload to send.
398
+ * @returns {EventResponse} - The EventResponse instance for chaining.
324
399
  */
325
- reject (message?: string, errorCode?: number): void;
400
+ reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
326
401
 
327
402
  /**
328
- * @desc Emits a direct message to the client
329
- * @param event - the event name
330
- * @param payload - the payload to send
331
- * @param assigns - the data to assign to the client
403
+ * @desc Sends a message to all clients in the channel.
404
+ * @param {string} event - The event to send.
405
+ * @param {PondMessage} payload - The payload to send.
406
+ * @returns {EventResponse} - The EventResponse instance for chaining.
332
407
  */
333
- send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
334
- }
408
+ broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
335
409
 
336
- export declare class PondChannel <EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
337
410
  /**
338
- * @desc Handles an event request made by a user
339
- * @param event - The event to listen for
340
- * @param handler - The handler to execute when the event is received
341
- * @example
342
- * pond.onEvent('echo', (request, response) => {
343
- * response.send('echo', {
344
- * message: request.event.payload,
345
- * });
346
- * });
411
+ * @desc Sends a message to all clients in the channel except the client making the request.
412
+ * @param {string} event - The event to send.
413
+ * @param {PondMessage} payload - The payload to send.
414
+ * @returns {EventResponse} - The EventResponse instance for chaining.
347
415
  */
348
- onEvent<Event extends string> (event: PondPath<Event>, handler: (request: EventRequest<Event, PresenceType, AssignType>, response: EventResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): void;
416
+ broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventResponse<EventType, PresenceType, AssignType>;
349
417
 
350
418
  /**
351
- * @desc Broadcasts a message to all users in a channel
352
- * @param event - The event to broadcast
353
- * @param payload - The payload to send
354
- * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
355
- * @example
356
- * pond.broadcast('echo', {
357
- * message: 'Hello World',
358
- * timestamp: Date.now(),
359
- * channel: 'my_channel',
360
- *});
419
+ * @desc Sends a message to a set of clients in the channel.
420
+ * @param {string} event - The event to send.
421
+ * @param {PondMessage} payload - The payload to send.
422
+ * @param {string | string[]} userIds - The ids of the clients to send the message to.
423
+ * @returns {EventResponse} - The EventResponse instance for chaining.
361
424
  */
362
- broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key], channelName?: string): void;
425
+ broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventResponse<EventType, PresenceType, AssignType>;
363
426
 
364
427
  /**
365
- * @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
366
- * @param callback - The callback to execute when a user leaves
428
+ * @desc Tracks a user's presence in the channel.
429
+ * @param {PondPresence} presence - The initial presence data.
430
+ * @param {string} userId - The id of the user to track.
431
+ * @returns {EventResponse} - The EventResponse instance for chaining.
367
432
  */
368
- public onLeave (callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
433
+ trackPresence(presence: PresenceType, userId?: string): EventResponse<EventType, PresenceType, AssignType>;
369
434
 
370
435
  /**
371
- * @desc Gets a channel by name
372
- * @param channelName - The name of the channel to get
436
+ * @desc Updates a user's presence in the channel.
437
+ * @param {PondPresence} presence - The updated presence data.
438
+ * @param {string} userId - The id of the user to update.
439
+ * @returns {EventResponse} - The EventResponse instance for chaining.
373
440
  */
374
- public getChannel (channelName: string): Channel<EventType, PresenceType, AssignType>;
375
- }
441
+ updatePresence(presence: PresenceType, userId?: string): EventResponse;
376
442
 
377
- export declare class PondSocket {
378
- constructor (server?: HTTPServer, socketServer?: WebSocketServer);
443
+ /**
444
+ * @desc Removes a user's presence from the channel.
445
+ * @param {string} userId - The id of the user to remove.
446
+ * @returns {EventResponse} - The EventResponse instance for chaining.
447
+ */
448
+ removePresence(userId?: string): EventResponse;
379
449
 
380
450
  /**
381
- * @desc Specifies the port to listen on
382
- * @param args - the arguments to pass to the server
451
+ * @desc Subscribes the client to a channel.
452
+ * @param {string} channel - The channel to subscribe to.
453
+ * @returns {EventResponse} - The EventResponse instance for chaining.
383
454
  */
384
- listen (...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
455
+ subscribeTo(channel: string): EventResponse;
385
456
 
386
457
  /**
387
- * @desc Closes the server
388
- * @param callback - the callback to call when the server is closed
458
+ * @desc Unsubscribes the client from a channel.
459
+ * @param {string} channel - The channel to unsubscribe from.
460
+ * @returns {EventResponse} - The EventResponse instance for chaining.
389
461
  */
390
- close (callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
462
+ unsubscribeFrom(channel: string): EventResponse;
391
463
 
392
464
  /**
393
- * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
394
- * @param path - the pattern to accept || can also be a regex
395
- * @param handler - the handler function to authenticate the socket
396
- * @example
397
- * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
398
- * const token = req.query.token;
399
- * if (!token)
400
- * return res.reject('No token provided');
401
- * res.accept({
402
- * assign: {
403
- * token
404
- * }
405
- * });
406
- * })
465
+ * @desc Evicts a user from the channel.
466
+ * @param {string} reason - The reason for the eviction.
467
+ * @param {string} userId - The id of the user to evict.
468
+ * @returns {EventResponse} - The EventResponse instance for chaining.
407
469
  */
408
- createEndpoint<Path extends string> (path: PondPath<Path>, handler: (request: IncomingConnection<Path>, response: ConnectionResponse) => void | Promise<void>): Endpoint;
470
+ evictUser(reason: string, userId?: string): EventResponse;
471
+
472
+ /**
473
+ * @desc Closes the channel from the server side for all clients.
474
+ * @param {string} reason - The reason for closing the channel.
475
+ */
476
+ closeChannel(reason: string): void;
409
477
  }
478
+