@eleven-am/pondsocket 0.1.68 → 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 +34 -14
- 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)
|
|
@@ -116,9 +131,13 @@ const message = "Hello, PondSocket!";
|
|
|
116
131
|
channel.broadcast('message', { text: message });
|
|
117
132
|
|
|
118
133
|
// Handle received messages
|
|
119
|
-
channel
|
|
134
|
+
// Certain methods in the channel instance returns a subscription function, which can be used to unsubscribe from the event
|
|
135
|
+
const subscription = channel.onMessage((event, message) => {
|
|
120
136
|
console.log(`Received message from server: ${message.text}`);
|
|
121
137
|
});
|
|
138
|
+
|
|
139
|
+
// Unsubscribe from the event
|
|
140
|
+
subscription();
|
|
122
141
|
```
|
|
123
142
|
|
|
124
143
|
The client will now connect to the server, and the server will receive the necessary headers automatically, including any authentication tokens or cookies, as required by the browser.
|
|
@@ -183,7 +202,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
183
202
|
const { username } = req.joinParams;
|
|
184
203
|
const { id } = req.event.params;
|
|
185
204
|
|
|
186
|
-
// maybe retrieve the
|
|
205
|
+
// maybe retrieve the previous messages from the database
|
|
187
206
|
const messages = await getMessagesFromDatabase(id);
|
|
188
207
|
|
|
189
208
|
// Check if the user has the required role to join the channel
|
|
@@ -233,7 +252,7 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
233
252
|
// you can broadcast a message to all users or In the channel that profanity is not allowed
|
|
234
253
|
res.broadcast('profanity-warning', { message: 'Profanity is not allowed' })
|
|
235
254
|
// or you can send a message to the user that profanity is not allowed
|
|
236
|
-
.
|
|
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]);
|
|
237
256
|
}
|
|
238
257
|
} else {
|
|
239
258
|
// Accept the message to allow broadcasting to other clients in the channel
|
|
@@ -257,15 +276,14 @@ profanityChannel.onEvent('presence/:presence', (req, res) => {
|
|
|
257
276
|
});
|
|
258
277
|
});
|
|
259
278
|
|
|
260
|
-
profanityChannel.onLeave((
|
|
261
|
-
const { username } =
|
|
279
|
+
profanityChannel.onLeave((event) => {
|
|
280
|
+
const { username } = event.assigns;
|
|
262
281
|
|
|
263
282
|
// When a user leaves the channel, PondSocket will automatically remove the user from the presence list and inform other users in the channel
|
|
264
283
|
|
|
265
284
|
// perform a cleanup operation here
|
|
266
285
|
});
|
|
267
286
|
|
|
268
|
-
|
|
269
287
|
// Start the server
|
|
270
288
|
pond.listen(3000, () => {
|
|
271
289
|
console.log('PondSocket server listening on port 3000');
|
|
@@ -274,8 +292,6 @@ pond.listen(3000, () => {
|
|
|
274
292
|
|
|
275
293
|
## API Documentation
|
|
276
294
|
|
|
277
|
-
Apologies for the confusion. Let me remove "Class" from the title of each section:
|
|
278
|
-
|
|
279
295
|
### PondSocket
|
|
280
296
|
|
|
281
297
|
The `PondSocket` class is the core class that represents the socket server.
|
|
@@ -414,12 +430,14 @@ The `EventResponse` class represents the response object for handling events fro
|
|
|
414
430
|
|
|
415
431
|
- `closeChannel(reason: string): void`: Closes the channel from the server-side for all clients.
|
|
416
432
|
|
|
417
|
-
###
|
|
433
|
+
### Channel
|
|
418
434
|
|
|
419
|
-
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.
|
|
420
436
|
|
|
421
437
|
**Methods:**
|
|
422
438
|
|
|
439
|
+
- `name: string`: The name of the channel.
|
|
440
|
+
|
|
423
441
|
- `getAssigns: UserAssigns`: Gets the current assign data for the client.
|
|
424
442
|
|
|
425
443
|
- `getUserData(userId: string): UserData`: Gets the assign data for a specific user identified by the provided `userId`.
|
|
@@ -428,7 +446,9 @@ The `Client` class represents a single Channel created by the PondSocket server.
|
|
|
428
446
|
|
|
429
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.
|
|
430
448
|
|
|
431
|
-
- `
|
|
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.
|
|
432
452
|
|
|
433
453
|
- `trackPresence(userId: string, presence: PondPresence): void`: Tracks a user's presence in the channel identified by the provided `userId`.
|
|
434
454
|
|
|
@@ -456,9 +476,9 @@ The `PondClient` class represents a client that connects to the PondSocket serve
|
|
|
456
476
|
|
|
457
477
|
- `onConnectionChange(callback: (state: boolean) => void): Unsubscribe`: Subscribes to the connection state changes and calls the provided callback when the state changes.
|
|
458
478
|
|
|
459
|
-
###
|
|
479
|
+
### ClientChannel
|
|
460
480
|
|
|
461
|
-
The `
|
|
481
|
+
The `ClientChannel` class represents a channel in the PondClient.
|
|
462
482
|
|
|
463
483
|
**Methods:**
|
|
464
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
|
+
|