@eleven-am/pondsocket 0.1.69 → 0.1.71
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/README.md +31 -13
- package/channel/channel.js +16 -22
- package/channel/eventResponse.js +8 -11
- package/lobby/joinResponse.js +1 -1
- package/package.json +13 -13
- package/types.d.ts +15 -1
package/README.md
CHANGED
|
@@ -27,6 +27,21 @@ const pond = new PondSocket();
|
|
|
27
27
|
const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
28
28
|
// Handle socket connection and authentication
|
|
29
29
|
});
|
|
30
|
+
|
|
31
|
+
// Start the server
|
|
32
|
+
pond.listen(3000);
|
|
33
|
+
|
|
34
|
+
// Or alternatively, working with express
|
|
35
|
+
import pondSocket from "@eleven-am/pondsocket/express";
|
|
36
|
+
import express from "express";
|
|
37
|
+
|
|
38
|
+
const app = pondSocket(express());
|
|
39
|
+
|
|
40
|
+
const endpoint = app.upgrade('/api/socket', (req, res) => {
|
|
41
|
+
// Handle socket connection and authentication
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
app.listen(3000);
|
|
30
45
|
```
|
|
31
46
|
|
|
32
47
|
Within each endpoint, sockets interact through channels. Channels provide an organized way to group users and manage efficient communication among them. When users join a channel, they can participate in real-time events and exchange information with other users in the same channel.
|
|
@@ -86,7 +101,7 @@ import PondClient from "@eleven-am/pondsocket/client";
|
|
|
86
101
|
// Your server URL
|
|
87
102
|
const serverUrl = 'ws://your-server-url/api/socket';
|
|
88
103
|
|
|
89
|
-
// Your authenticated user's
|
|
104
|
+
// Your authenticated user's token (replace with actual token)
|
|
90
105
|
const authToken = 'your-auth-token';
|
|
91
106
|
|
|
92
107
|
// Your username (replace with actual username)
|
|
@@ -187,7 +202,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
187
202
|
const { username } = req.joinParams;
|
|
188
203
|
const { id } = req.event.params;
|
|
189
204
|
|
|
190
|
-
// maybe retrieve the
|
|
205
|
+
// maybe retrieve the previous messages from the database
|
|
191
206
|
const messages = await getMessagesFromDatabase(id);
|
|
192
207
|
|
|
193
208
|
// Check if the user has the required role to join the channel
|
|
@@ -237,14 +252,14 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
237
252
|
// you can broadcast a message to all users or In the channel that profanity is not allowed
|
|
238
253
|
res.broadcast('profanity-warning', { message: 'Profanity is not allowed' })
|
|
239
254
|
// or you can send a message to the user that profanity is not allowed
|
|
240
|
-
.
|
|
255
|
+
.sendToUsers('profanity-warning', { message: `You have used profanity ${profanityCount} times. You will be kicked from the channel if you use profanity more than 3 times.` }, [req.user.id]);
|
|
241
256
|
}
|
|
242
257
|
} else {
|
|
243
258
|
// Accept the message to allow broadcasting to other clients in the channel
|
|
244
259
|
res.accept();
|
|
245
260
|
}
|
|
246
261
|
|
|
247
|
-
// for more complete access to the channel, you can use the
|
|
262
|
+
// for more complete access to the channel, you can use the channel instance
|
|
248
263
|
// const channel = req.channel;
|
|
249
264
|
});
|
|
250
265
|
|
|
@@ -269,7 +284,6 @@ profanityChannel.onLeave((event) => {
|
|
|
269
284
|
// perform a cleanup operation here
|
|
270
285
|
});
|
|
271
286
|
|
|
272
|
-
|
|
273
287
|
// Start the server
|
|
274
288
|
pond.listen(3000, () => {
|
|
275
289
|
console.log('PondSocket server listening on port 3000');
|
|
@@ -336,7 +350,7 @@ The `JoinRequest` class represents the request object when a client joins a chan
|
|
|
336
350
|
|
|
337
351
|
- `user: UserData`: The user data associated with the client.
|
|
338
352
|
|
|
339
|
-
- `
|
|
353
|
+
- `channel: Channel`: The Channel instance associated with the request.
|
|
340
354
|
|
|
341
355
|
### JoinResponse
|
|
342
356
|
|
|
@@ -386,7 +400,7 @@ The `EventRequest` class represents the request object when an event is received
|
|
|
386
400
|
|
|
387
401
|
- `user: UserData`: The user data associated with the client.
|
|
388
402
|
|
|
389
|
-
- `
|
|
403
|
+
- `channel: Channel`: The Channel instance associated with the request.
|
|
390
404
|
|
|
391
405
|
### EventResponse
|
|
392
406
|
|
|
@@ -416,12 +430,14 @@ The `EventResponse` class represents the response object for handling events fro
|
|
|
416
430
|
|
|
417
431
|
- `closeChannel(reason: string): void`: Closes the channel from the server-side for all clients.
|
|
418
432
|
|
|
419
|
-
###
|
|
433
|
+
### Channel
|
|
420
434
|
|
|
421
|
-
The `
|
|
435
|
+
The `Channel` class represents a single Channel created by the PondSocket server. Note that a PondChannel can have multiple channels associated with it.
|
|
422
436
|
|
|
423
437
|
**Methods:**
|
|
424
438
|
|
|
439
|
+
- `name: string`: The name of the channel.
|
|
440
|
+
|
|
425
441
|
- `getAssigns: UserAssigns`: Gets the current assign data for the client.
|
|
426
442
|
|
|
427
443
|
- `getUserData(userId: string): UserData`: Gets the assign data for a specific user identified by the provided `userId`.
|
|
@@ -430,7 +446,9 @@ The `Client` class represents a single Channel created by the PondSocket server.
|
|
|
430
446
|
|
|
431
447
|
- `sendToUser(userId: string, event: string, payload: PondMessage): void`: Sends a message to a specific client in the channel identified by the provided `userId`, with the specified event and payload.
|
|
432
448
|
|
|
433
|
-
- `
|
|
449
|
+
- `sendToUsers(userIdS: string[], event: string, payload: PondMessage): void`: Sends a message to a specific set of clients identified by the provided `userIdS`, with the specified event and payload.
|
|
450
|
+
|
|
451
|
+
- `evictUser(userId: string, reason?: string): void`: Bans a user from the channel identified by the provided `userId`. Optionally, you can provide a `reason` for the ban.
|
|
434
452
|
|
|
435
453
|
- `trackPresence(userId: string, presence: PondPresence): void`: Tracks a user's presence in the channel identified by the provided `userId`.
|
|
436
454
|
|
|
@@ -454,13 +472,13 @@ The `PondClient` class represents a client that connects to the PondSocket serve
|
|
|
454
472
|
|
|
455
473
|
- `disconnect(): void`: Disconnects the socket.
|
|
456
474
|
|
|
457
|
-
- `createChannel(name: string, params?: JoinParams):
|
|
475
|
+
- `createChannel(name: string, params?: JoinParams): ClientChannel`: Creates a channel with the given name and optional join parameters.
|
|
458
476
|
|
|
459
477
|
- `onConnectionChange(callback: (state: boolean) => void): Unsubscribe`: Subscribes to the connection state changes and calls the provided callback when the state changes.
|
|
460
478
|
|
|
461
|
-
###
|
|
479
|
+
### ClientChannel
|
|
462
480
|
|
|
463
|
-
The `
|
|
481
|
+
The `ClientChannel` class represents a channel in the PondClient.
|
|
464
482
|
|
|
465
483
|
**Methods:**
|
|
466
484
|
|
package/channel/channel.js
CHANGED
|
@@ -91,7 +91,8 @@ class ChannelEngine {
|
|
|
91
91
|
*/
|
|
92
92
|
kickUser(userId, reason) {
|
|
93
93
|
this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.SYSTEM, 'kicked_out', {
|
|
94
|
-
message: reason
|
|
94
|
+
message: reason,
|
|
95
|
+
code: 403,
|
|
95
96
|
});
|
|
96
97
|
this.removeUser(userId);
|
|
97
98
|
this.sendMessage(enums_1.SystemSender.CHANNEL, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.SYSTEM, 'kicked', {
|
|
@@ -157,7 +158,6 @@ class ChannelEngine {
|
|
|
157
158
|
* @param action - The action of the message
|
|
158
159
|
* @param event - The event name
|
|
159
160
|
* @param payload - The payload of the message
|
|
160
|
-
* @private
|
|
161
161
|
*/
|
|
162
162
|
sendMessage(sender, recipient, action, event, payload) {
|
|
163
163
|
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(sender) && sender !== enums_1.SystemSender.CHANNEL) {
|
|
@@ -198,24 +198,14 @@ class ChannelEngine {
|
|
|
198
198
|
});
|
|
199
199
|
});
|
|
200
200
|
}
|
|
201
|
-
/**
|
|
202
|
-
* @desc Begins tracking a user's presence
|
|
203
|
-
* @param userId - The id of the user to track
|
|
204
|
-
* @param presence - The initial presence of the user
|
|
205
|
-
*/
|
|
206
|
-
trackPresence(userId, presence) {
|
|
207
|
-
var _a;
|
|
208
|
-
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(userId)) {
|
|
209
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
210
|
-
}
|
|
211
|
-
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this), "f");
|
|
212
|
-
__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").trackPresence(userId, presence);
|
|
213
|
-
}
|
|
214
201
|
/**
|
|
215
202
|
* @desc Gets the presence engine for the channel
|
|
216
203
|
*/
|
|
217
204
|
get presenceEngine() {
|
|
218
|
-
|
|
205
|
+
var _a;
|
|
206
|
+
const presenceEngine = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this);
|
|
207
|
+
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, presenceEngine, "f");
|
|
208
|
+
return presenceEngine;
|
|
219
209
|
}
|
|
220
210
|
/**
|
|
221
211
|
* @desc Gets the number of users in the channel
|
|
@@ -262,6 +252,9 @@ class Channel {
|
|
|
262
252
|
_Channel_engine.set(this, void 0);
|
|
263
253
|
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
264
254
|
}
|
|
255
|
+
get name() {
|
|
256
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").name;
|
|
257
|
+
}
|
|
265
258
|
getAssigns() {
|
|
266
259
|
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
267
260
|
}
|
|
@@ -274,19 +267,20 @@ class Channel {
|
|
|
274
267
|
sendToUser(userId, event, payload) {
|
|
275
268
|
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.BROADCAST, event, payload);
|
|
276
269
|
}
|
|
277
|
-
|
|
270
|
+
sendToUsers(userIds, event, payload) {
|
|
271
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, userIds, enums_1.ServerActions.BROADCAST, event, payload);
|
|
272
|
+
}
|
|
273
|
+
evictUser(userId, reason) {
|
|
278
274
|
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
279
275
|
}
|
|
280
276
|
trackPresence(userId, presence) {
|
|
281
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").trackPresence(userId, presence);
|
|
277
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
282
278
|
}
|
|
283
279
|
removePresence(userId) {
|
|
284
|
-
|
|
285
|
-
(_a = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
280
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
|
|
286
281
|
}
|
|
287
282
|
updatePresence(userId, presence) {
|
|
288
|
-
|
|
289
|
-
(_a = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence(userId, presence);
|
|
283
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
290
284
|
}
|
|
291
285
|
}
|
|
292
286
|
exports.Channel = Channel;
|
package/channel/eventResponse.js
CHANGED
|
@@ -94,8 +94,8 @@ class EventResponse extends abstractResponse_1.PondResponse {
|
|
|
94
94
|
* @param presence - the initial presence data
|
|
95
95
|
* @param userId - the id of the user to track
|
|
96
96
|
*/
|
|
97
|
-
trackPresence(presence, userId) {
|
|
98
|
-
__classPrivateFieldGet(this, _EventResponse_engine, "f").trackPresence(userId
|
|
97
|
+
trackPresence(presence, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
|
|
98
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
99
99
|
return this;
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
@@ -103,19 +103,16 @@ class EventResponse extends abstractResponse_1.PondResponse {
|
|
|
103
103
|
* @param presence - the updated presence data
|
|
104
104
|
* @param userId - the id of the user to update
|
|
105
105
|
*/
|
|
106
|
-
updatePresence(presence, userId) {
|
|
107
|
-
|
|
108
|
-
(_a = __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence(userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender, presence);
|
|
106
|
+
updatePresence(presence, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
|
|
107
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
109
108
|
return this;
|
|
110
109
|
}
|
|
111
110
|
/**
|
|
112
111
|
* @desc Removes a user's presence from the channel
|
|
113
112
|
* @param userId - the id of the user to remove
|
|
114
113
|
*/
|
|
115
|
-
unTrackPresence(userId) {
|
|
116
|
-
|
|
117
|
-
userId = userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender;
|
|
118
|
-
(_a = __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
114
|
+
unTrackPresence(userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
|
|
115
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.removePresence(userId);
|
|
119
116
|
return this;
|
|
120
117
|
}
|
|
121
118
|
/**
|
|
@@ -123,8 +120,8 @@ class EventResponse extends abstractResponse_1.PondResponse {
|
|
|
123
120
|
* @param reason - the reason for the eviction
|
|
124
121
|
* @param userId - the id of the user to evict,
|
|
125
122
|
*/
|
|
126
|
-
evictUser(reason, userId) {
|
|
127
|
-
__classPrivateFieldGet(this, _EventResponse_engine, "f").kickUser(userId
|
|
123
|
+
evictUser(reason, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
|
|
124
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").kickUser(userId, reason);
|
|
128
125
|
}
|
|
129
126
|
/**
|
|
130
127
|
* @desc Closes the channel from the server side for all clients
|
package/lobby/joinResponse.js
CHANGED
|
@@ -110,7 +110,7 @@ class JoinResponse extends abstractResponse_1.PondResponse {
|
|
|
110
110
|
* @param presence - the presence data to track
|
|
111
111
|
*/
|
|
112
112
|
trackPresence(presence) {
|
|
113
|
-
__classPrivateFieldGet(this, _JoinResponse_engine, "f").trackPresence(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, presence);
|
|
113
|
+
__classPrivateFieldGet(this, _JoinResponse_engine, "f").presenceEngine.trackPresence(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, presence);
|
|
114
114
|
return this;
|
|
115
115
|
}
|
|
116
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.71",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -30,22 +30,22 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"websocket": "^1.0.34",
|
|
33
|
-
"ws": "^8.
|
|
33
|
+
"ws": "^8.15.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/express": "^4.17.
|
|
37
|
-
"@types/jest": "^29.5.
|
|
38
|
-
"@types/node": "^20.
|
|
39
|
-
"@types/websocket": "^1.0.
|
|
40
|
-
"@types/ws": "^8.5.
|
|
41
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
42
|
-
"eslint": "^8.
|
|
36
|
+
"@types/express": "^4.17.21",
|
|
37
|
+
"@types/jest": "^29.5.11",
|
|
38
|
+
"@types/node": "^20.10.4",
|
|
39
|
+
"@types/websocket": "^1.0.10",
|
|
40
|
+
"@types/ws": "^8.5.10",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^6.13.2",
|
|
42
|
+
"eslint": "^8.55.0",
|
|
43
43
|
"eslint-plugin-file-progress": "^1.3.0",
|
|
44
|
-
"eslint-plugin-import": "^2.
|
|
45
|
-
"jest": "^29.
|
|
44
|
+
"eslint-plugin-import": "^2.29.0",
|
|
45
|
+
"jest": "^29.7.0",
|
|
46
46
|
"superwstest": "^2.0.3",
|
|
47
47
|
"ts-jest": "^29.1.1",
|
|
48
|
-
"ts-node": "^10.9.
|
|
49
|
-
"typescript": "^5.
|
|
48
|
+
"ts-node": "^10.9.2",
|
|
49
|
+
"typescript": "^5.3.3"
|
|
50
50
|
}
|
|
51
51
|
}
|
package/types.d.ts
CHANGED
|
@@ -320,6 +320,11 @@ declare class Endpoint {
|
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
export declare class Channel {
|
|
323
|
+
/**
|
|
324
|
+
* The name of the channel.
|
|
325
|
+
*/
|
|
326
|
+
name: string;
|
|
327
|
+
|
|
323
328
|
/**
|
|
324
329
|
* @desc Gets the current assign data for the channel.
|
|
325
330
|
*/
|
|
@@ -346,12 +351,20 @@ export declare class Channel {
|
|
|
346
351
|
*/
|
|
347
352
|
sendToUser (userId: string, event: string, payload: PondMessage): void;
|
|
348
353
|
|
|
354
|
+
/**
|
|
355
|
+
* @desc Sends a message to specific clients in the channel.
|
|
356
|
+
* @param userIds - The ids of the users to send the message to.
|
|
357
|
+
* @param event - The event to send.
|
|
358
|
+
* @param payload - The message to send.
|
|
359
|
+
*/
|
|
360
|
+
sendToUsers (userIds: string[], event: string, payload: PondMessage): void;
|
|
361
|
+
|
|
349
362
|
/**
|
|
350
363
|
* @desc Bans a user from the channel.
|
|
351
364
|
* @param userId - The id of the user to ban.
|
|
352
365
|
* @param reason - The reason for the ban.
|
|
353
366
|
*/
|
|
354
|
-
|
|
367
|
+
evictUser (userId: string, reason?: string): void;
|
|
355
368
|
|
|
356
369
|
/**
|
|
357
370
|
* @desc tracks a user's presence in the channel
|
|
@@ -581,3 +594,4 @@ declare class PondClient {
|
|
|
581
594
|
}
|
|
582
595
|
|
|
583
596
|
declare const pondSocket: (app: Express) => PondSocketExpressApp;
|
|
597
|
+
|