@eleven-am/pondsocket 0.1.69 → 0.1.70
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 +27 -9
- package/channel/channel.js +7 -2
- package/package.json +1 -1
- 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,7 +252,7 @@ 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
|
|
@@ -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');
|
|
@@ -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
|
|
|
@@ -458,9 +476,9 @@ The `PondClient` class represents a client that connects to the PondSocket serve
|
|
|
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
|
@@ -157,7 +157,6 @@ class ChannelEngine {
|
|
|
157
157
|
* @param action - The action of the message
|
|
158
158
|
* @param event - The event name
|
|
159
159
|
* @param payload - The payload of the message
|
|
160
|
-
* @private
|
|
161
160
|
*/
|
|
162
161
|
sendMessage(sender, recipient, action, event, payload) {
|
|
163
162
|
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(sender) && sender !== enums_1.SystemSender.CHANNEL) {
|
|
@@ -262,6 +261,9 @@ class Channel {
|
|
|
262
261
|
_Channel_engine.set(this, void 0);
|
|
263
262
|
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
264
263
|
}
|
|
264
|
+
get name() {
|
|
265
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").name;
|
|
266
|
+
}
|
|
265
267
|
getAssigns() {
|
|
266
268
|
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
267
269
|
}
|
|
@@ -274,7 +276,10 @@ class Channel {
|
|
|
274
276
|
sendToUser(userId, event, payload) {
|
|
275
277
|
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.BROADCAST, event, payload);
|
|
276
278
|
}
|
|
277
|
-
|
|
279
|
+
sendToUsers(userIds, event, payload) {
|
|
280
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, userIds, enums_1.ServerActions.BROADCAST, event, payload);
|
|
281
|
+
}
|
|
282
|
+
evictUser(userId, reason) {
|
|
278
283
|
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
279
284
|
}
|
|
280
285
|
trackPresence(userId, presence) {
|
package/package.json
CHANGED
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
|
+
|