@eleven-am/pondsocket-client 0.0.12 → 0.0.14

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 CHANGED
@@ -175,7 +175,7 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
175
175
  const role = getRoleFromToken(token);
176
176
 
177
177
  // Handle socket connection and authentication for valid users
178
- res.accept({ role }); // Assign the user's role to the socket
178
+ res.accept({role}); // Assign the user's role to the socket
179
179
  } else {
180
180
  // Reject the connection for invalid users or without a token
181
181
  res.reject('Invalid token', 401);
@@ -186,9 +186,9 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
186
186
  const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res) => {
187
187
  // When joining the channel, any joinParams passed from the client will be available in the request payload
188
188
  // Also any previous assigns on the socket will be available in the request payload as well
189
- const { role } = req.user.assigns;
190
- const { username } = req.joinParams;
191
- const { id } = req.event.params;
189
+ const {role} = req.user.assigns;
190
+ const {username} = req.joinParams;
191
+ const {id} = req.event.params;
192
192
 
193
193
  // maybe retrieve the previous messages from the database
194
194
  const messages = await getMessagesFromDatabase(id);
@@ -196,7 +196,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
196
196
  // Check if the user has the required role to join the channel
197
197
  if (role === 'admin') {
198
198
  // Accept the join request
199
- res.accept({ username, profanityCount: 0 })
199
+ res.accept({username, profanityCount: 0})
200
200
  // optionally you can track the presence of the user in the channel
201
201
  .trackPresence({
202
202
  username,
@@ -205,7 +205,7 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
205
205
  onlineSince: Date.now(),
206
206
  })
207
207
  // and send the user the channel history
208
- .sendToUsers('history', { messages }, [req.user.id]);
208
+ .sendToUsers('history', {messages}, [req.user.id]);
209
209
 
210
210
  // Alternatively, you can also send messages to the user, NOTE that the user would be automatically subscribed to the channel.
211
211
  // res.send('history', { messages }, { username, profanityCount: 0 })
@@ -217,19 +217,19 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
217
217
  // });
218
218
  } else {
219
219
  // Reject the join request
220
- res.reject('You do not have the required role to join this channel', 403);
220
+ res.decline('You do not have the required role to join this channel', 403);
221
221
  }
222
222
  });
223
223
 
224
224
  // Attach message event listener to the profanityChannel
225
225
  profanityChannel.onEvent('message', (req, res) => {
226
- const { text } = req.event.payload;
226
+ const {text} = req.event.payload;
227
227
 
228
228
  // Check for profanity
229
229
  if (isTextProfane(text)) {
230
230
  // Reject the message if it contains profanity
231
- res.reject('Profanity is not allowed', 400, {
232
- profanityCount: req.user.assigns.profanityCount + 1
231
+ res.decline('Profanity is not allowed', 400, {
232
+ profanityCount: req.user.assigns.profanityCount + 1
233
233
  });
234
234
 
235
235
  // note that profanityCount is updated so req.user.assigns.profanityCount will be updated
@@ -238,9 +238,9 @@ profanityChannel.onEvent('message', (req, res) => {
238
238
  res.evictUser('You have been kicked from the channel for using profanity');
239
239
  } else {
240
240
  // you can broadcast a message to all users or In the channel that profanity is not allowed
241
- res.broadcast('profanity-warning', { message: 'Profanity is not allowed' })
241
+ res.broadcast('profanity-warning', {message: 'Profanity is not allowed'})
242
242
  // or you can send a message to the user that profanity is not allowed
243
- .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]);
243
+ .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]);
244
244
  }
245
245
  } else {
246
246
  // Accept the message to allow broadcasting to other clients in the channel
@@ -252,8 +252,8 @@ profanityChannel.onEvent('message', (req, res) => {
252
252
  });
253
253
 
254
254
  profanityChannel.onEvent('presence/:presence', (req, res) => {
255
- const { presence } = req.event.params;
256
- const { username } = req.user.assigns;
255
+ const {presence} = req.event.params;
256
+ const {username} = req.user.assigns;
257
257
 
258
258
  // Handle presence events
259
259
  res.updatePresence({
@@ -265,7 +265,7 @@ profanityChannel.onEvent('presence/:presence', (req, res) => {
265
265
  });
266
266
 
267
267
  profanityChannel.onLeave((event) => {
268
- const { username } = event.assigns;
268
+ const {username} = event.assigns;
269
269
 
270
270
  // When a user leaves the channel, PondSocket will automatically remove the user from the presence list and inform other users in the channel
271
271
 
package/browser/client.js CHANGED
@@ -34,8 +34,8 @@ class PondClient {
34
34
  }
35
35
  this._address = address;
36
36
  __classPrivateFieldSet(this, _PondClient_channels, {}, "f");
37
- this._broadcaster = new pondsocket_common_1.SimpleSubject();
38
- this._connectionState = new pondsocket_common_1.SimpleBehaviorSubject(false);
37
+ this._broadcaster = new pondsocket_common_1.Subject();
38
+ this._connectionState = new pondsocket_common_1.BehaviorSubject(false);
39
39
  }
40
40
  /**
41
41
  * @desc Connects to the server and returns the socket.
@@ -89,7 +89,6 @@ describe('PondClient', () => {
89
89
  const sentObject = mockWebSocket.send.mock.calls[0][0];
90
90
  expect(sentObject).toBeDefined();
91
91
  expect(JSON.parse(sentObject)).toEqual(expect.objectContaining({
92
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
93
92
  action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
94
93
  event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
95
94
  payload: {},
package/core/channel.js CHANGED
@@ -32,8 +32,8 @@ class Channel {
32
32
  __classPrivateFieldSet(this, _Channel_joinParams, params, "f");
33
33
  __classPrivateFieldSet(this, _Channel_publisher, publisher, "f");
34
34
  __classPrivateFieldSet(this, _Channel_clientState, clientState, "f");
35
- __classPrivateFieldSet(this, _Channel_receiver, new pondsocket_common_1.SimpleSubject(), "f");
36
- __classPrivateFieldSet(this, _Channel_joinState, new pondsocket_common_1.SimpleBehaviorSubject(pondsocket_common_1.ChannelState.IDLE), "f");
35
+ __classPrivateFieldSet(this, _Channel_receiver, new pondsocket_common_1.Subject(), "f");
36
+ __classPrivateFieldSet(this, _Channel_joinState, new pondsocket_common_1.BehaviorSubject(pondsocket_common_1.ChannelState.IDLE), "f");
37
37
  __classPrivateFieldSet(this, _Channel_presenceSub, __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_init).call(this, receiver), "f");
38
38
  }
39
39
  /**
@@ -63,7 +63,6 @@ class Channel {
63
63
  */
64
64
  leave() {
65
65
  const leaveMessage = {
66
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
67
66
  action: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
68
67
  event: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
69
68
  channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
@@ -141,11 +140,10 @@ class Channel {
141
140
  * @desc Sends a message to specific clients in the channel.
142
141
  * @param event - The event to send.
143
142
  * @param payload - The message to send.
144
- * @param recipient - The clients to send the message to.
145
143
  */
146
- sendMessage(event, payload, recipient) {
144
+ sendMessage(event, payload) {
147
145
  const requestId = (0, pondsocket_common_1.uuid)();
148
- __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload, recipient);
146
+ __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload);
149
147
  }
150
148
  /**
151
149
  * @desc Sends a message to the server and waits for a response.
@@ -164,24 +162,6 @@ class Channel {
164
162
  __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, sentEvent, requestId, payload);
165
163
  });
166
164
  }
167
- /**
168
- * @desc Broadcasts a message to every other client in the channel except yourself.
169
- * @param event - The event to send.
170
- * @param payload - The message to send.
171
- */
172
- broadcastFrom(event, payload) {
173
- const requestId = (0, pondsocket_common_1.uuid)();
174
- __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER);
175
- }
176
- /**
177
- * @desc Broadcasts a message to the channel, including yourself.
178
- * @param event - The event to send.
179
- * @param payload - The message to send.
180
- */
181
- broadcast(event, payload) {
182
- const requestId = (0, pondsocket_common_1.uuid)();
183
- __classPrivateFieldGet(this, _Channel_instances, "m", _Channel_send).call(this, event, requestId, payload);
184
- }
185
165
  /**
186
166
  * @desc Gets the current presence of the channel.
187
167
  */
@@ -224,11 +204,10 @@ class Channel {
224
204
  }
225
205
  }
226
206
  exports.Channel = Channel;
227
- _Channel_name = new WeakMap(), _Channel_queue = new WeakMap(), _Channel_presence = new WeakMap(), _Channel_publisher = new WeakMap(), _Channel_joinParams = new WeakMap(), _Channel_presenceSub = new WeakMap(), _Channel_receiver = new WeakMap(), _Channel_clientState = new WeakMap(), _Channel_joinState = new WeakMap(), _Channel_instances = new WeakSet(), _Channel_send = function _Channel_send(event, requestId, payload, receivers = pondsocket_common_1.ChannelReceiver.ALL_USERS) {
207
+ _Channel_name = new WeakMap(), _Channel_queue = new WeakMap(), _Channel_presence = new WeakMap(), _Channel_publisher = new WeakMap(), _Channel_joinParams = new WeakMap(), _Channel_presenceSub = new WeakMap(), _Channel_receiver = new WeakMap(), _Channel_clientState = new WeakMap(), _Channel_joinState = new WeakMap(), _Channel_instances = new WeakSet(), _Channel_send = function _Channel_send(event, requestId, payload) {
228
208
  const message = {
229
209
  action: pondsocket_common_1.ClientActions.BROADCAST,
230
210
  channelName: __classPrivateFieldGet(this, _Channel_name, "f"),
231
- addresses: receivers,
232
211
  requestId,
233
212
  event,
234
213
  payload,
@@ -286,7 +265,6 @@ _Channel_name = new WeakMap(), _Channel_queue = new WeakMap(), _Channel_presence
286
265
  __classPrivateFieldSet(this, _Channel_queue, [], "f");
287
266
  }, _Channel_buildJoinMessage = function _Channel_buildJoinMessage() {
288
267
  return {
289
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
290
268
  action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
291
269
  event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
292
270
  payload: __classPrivateFieldGet(this, _Channel_joinParams, "f"),
@@ -13,8 +13,8 @@ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
13
13
  const channel_1 = require("./channel");
14
14
  const createChannel = (params = {}) => {
15
15
  const publisher = jest.fn();
16
- const state = new pondsocket_common_1.SimpleBehaviorSubject(true);
17
- const receiver = new pondsocket_common_1.SimpleSubject();
16
+ const state = new pondsocket_common_1.BehaviorSubject(true);
17
+ const receiver = new pondsocket_common_1.Subject();
18
18
  const channel = new channel_1.Channel(publisher, state, 'test', receiver, params);
19
19
  return {
20
20
  channel,
@@ -32,7 +32,6 @@ describe('Channel', () => {
32
32
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
33
33
  action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
34
34
  channelName: 'test',
35
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
36
35
  event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
37
36
  payload: {},
38
37
  }));
@@ -46,7 +45,6 @@ describe('Channel', () => {
46
45
  expect(publisher2).toHaveBeenCalledWith(expect.objectContaining({
47
46
  action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
48
47
  channelName: 'test',
49
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
50
48
  event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
51
49
  payload: {},
52
50
  }));
@@ -86,7 +84,6 @@ describe('Channel', () => {
86
84
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
87
85
  action: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
88
86
  channelName: 'test',
89
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
90
87
  event: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
91
88
  payload: {},
92
89
  }));
@@ -165,7 +162,7 @@ describe('Channel', () => {
165
162
  expect(channel.channelState).toBe(pondsocket_common_1.ChannelState.IDLE);
166
163
  expect(publisher).not.toHaveBeenCalled();
167
164
  // when the socket is connected but the channel is not joined, the message would not be queued
168
- channel.broadcast('test', { test: false });
165
+ channel.sendMessage('test', { test: false });
169
166
  expect(publisher).not.toHaveBeenCalled();
170
167
  channel.join();
171
168
  publisher.mockClear();
@@ -179,10 +176,9 @@ describe('Channel', () => {
179
176
  },
180
177
  });
181
178
  // however once the channel is joined, the message should be sent
182
- channel.broadcast('test', { test: true });
179
+ channel.sendMessage('test', { test: true });
183
180
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
184
181
  action: pondsocket_common_1.ClientActions.BROADCAST,
185
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
186
182
  channelName: 'test',
187
183
  event: 'test',
188
184
  payload: {
@@ -193,13 +189,12 @@ describe('Channel', () => {
193
189
  publisher.mockClear();
194
190
  state.publish(false);
195
191
  expect(state.value).toBe(false);
196
- channel.broadcast('test', { test: true });
192
+ channel.sendMessage('test', { test: true });
197
193
  expect(publisher).not.toHaveBeenCalled();
198
194
  // once the socket is reconnected, a join message should be sent and the queued messages should be sent
199
195
  publisher.mockClear();
200
196
  state.publish(true);
201
197
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
202
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
203
198
  action: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
204
199
  channelName: 'test',
205
200
  event: pondsocket_common_1.ClientActions.JOIN_CHANNEL,
@@ -218,7 +213,6 @@ describe('Channel', () => {
218
213
  });
219
214
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
220
215
  action: pondsocket_common_1.ClientActions.BROADCAST,
221
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
222
216
  channelName: 'test',
223
217
  event: 'test',
224
218
  payload: {
@@ -233,11 +227,10 @@ describe('Channel', () => {
233
227
  action: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
234
228
  channelName: 'test',
235
229
  event: pondsocket_common_1.ClientActions.LEAVE_CHANNEL,
236
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
237
230
  payload: {},
238
231
  }));
239
232
  expect(channel.channelState).toBe(pondsocket_common_1.ChannelState.CLOSED);
240
- channel.broadcast('test', { test: true });
233
+ channel.sendMessage('test', { test: true });
241
234
  expect(publisher).toHaveBeenCalledTimes(1);
242
235
  });
243
236
  // The presence system tests
@@ -670,7 +663,7 @@ describe('Channel', () => {
670
663
  });
671
664
  channel.sendMessage('test', {
672
665
  test: 'test',
673
- }, ['test1']);
666
+ });
674
667
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
675
668
  action: pondsocket_common_1.ClientActions.BROADCAST,
676
669
  channelName: 'test',
@@ -678,10 +671,9 @@ describe('Channel', () => {
678
671
  payload: {
679
672
  test: 'test',
680
673
  },
681
- addresses: ['test1'],
682
674
  }));
683
675
  publisher.mockClear();
684
- channel.broadcastFrom('test', {
676
+ channel.sendMessage('test', {
685
677
  test: 'test',
686
678
  });
687
679
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
@@ -691,10 +683,9 @@ describe('Channel', () => {
691
683
  payload: {
692
684
  test: 'test',
693
685
  },
694
- addresses: pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER,
695
686
  }));
696
687
  publisher.mockClear();
697
- channel.broadcast('test', {
688
+ channel.sendMessage('test', {
698
689
  test: 'test',
699
690
  });
700
691
  expect(publisher).toHaveBeenCalledWith(expect.objectContaining({
@@ -704,12 +695,11 @@ describe('Channel', () => {
704
695
  payload: {
705
696
  test: 'test',
706
697
  },
707
- addresses: pondsocket_common_1.ChannelReceiver.ALL_USERS,
708
698
  }));
709
699
  });
710
700
  it('should be able to wait for a message', () => __awaiter(void 0, void 0, void 0, function* () {
711
- const state = new pondsocket_common_1.SimpleBehaviorSubject(true);
712
- const receiver = new pondsocket_common_1.SimpleSubject();
701
+ const state = new pondsocket_common_1.BehaviorSubject(true);
702
+ const receiver = new pondsocket_common_1.Subject();
713
703
  const params = {};
714
704
  const publisher = (data) => {
715
705
  if (data.event === 'WAITING') {
package/dist.d.ts CHANGED
@@ -1,21 +1,20 @@
1
1
  import {
2
- PondEventMap,
2
+ ChannelState,
3
+ EventWithResponse,
3
4
  JoinParams,
5
+ PayloadForResponse,
6
+ PondEventMap,
4
7
  PondPresence,
5
8
  PresencePayload,
6
- PondMessage,
7
- EventWithResponse,
8
- PayloadForResponse,
9
9
  ResponseForEvent,
10
10
  Unsubscribe,
11
- ChannelState,
12
11
  } from '@eleven-am/pondsocket-common';
13
12
 
14
13
  declare class Channel<EventMap extends PondEventMap = PondEventMap, Presence extends PondPresence = PondPresence> {
15
14
  /**
16
- * @desc Gets the current connection state of the channel.
15
+ * @desc The current connection state of the channel.
17
16
  */
18
- get channelState (): ChannelState;
17
+ channelState: ChannelState;
19
18
 
20
19
  /**
21
20
  * @desc Connects to the channel.
@@ -68,9 +67,8 @@ declare class Channel<EventMap extends PondEventMap = PondEventMap, Presence ext
68
67
  * @desc Sends a message to specific clients in the channel.
69
68
  * @param event - The event to send.
70
69
  * @param payload - The message to send.
71
- * @param recipient - The clients to send the message to.
72
70
  */
73
- sendMessage (event: string, payload: PondMessage, recipient: string[]): void;
71
+ sendMessage<Event extends keyof EventMap> (event: Event, payload: EventMap[Event]): void;
74
72
 
75
73
  /**
76
74
  * @desc Sends a message to the server and waits for a response.
@@ -79,20 +77,6 @@ declare class Channel<EventMap extends PondEventMap = PondEventMap, Presence ext
79
77
  */
80
78
  sendForResponse<Event extends EventWithResponse<EventMap>> (sentEvent: Event, payload: PayloadForResponse<EventMap, Event>): Promise<ResponseForEvent<EventMap, Event>>;
81
79
 
82
- /**
83
- * @desc Broadcasts a message to every other client in the channel except yourself.
84
- * @param event - The event to send.
85
- * @param payload - The message to send.
86
- */
87
- broadcastFrom<Event extends keyof EventMap> (event: Event, payload: EventMap[Event]): void;
88
-
89
- /**
90
- * @desc Broadcasts a message to the channel, including yourself.
91
- * @param event - The event to send.
92
- * @param payload - The message to send.
93
- */
94
- broadcast<Event extends keyof EventMap> (event: Event, payload: EventMap[Event]): void;
95
-
96
80
  /**
97
81
  * @desc Gets the current presence of the channel.
98
82
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket-client",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -29,19 +29,19 @@
29
29
  "pipeline": "npm run test && npm run build && npm run push"
30
30
  },
31
31
  "dependencies": {
32
- "@eleven-am/pondsocket-common": "^0.0.10",
32
+ "@eleven-am/pondsocket-common": "^0.0.15",
33
33
  "websocket": "^1.0.34"
34
34
  },
35
35
  "devDependencies": {
36
- "@types/jest": "^29.5.11",
36
+ "@types/jest": "^29.5.12",
37
37
  "@types/websocket": "^1.0.10",
38
- "@typescript-eslint/eslint-plugin": "^6.19.0",
38
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
39
39
  "eslint-plugin-file-progress": "^1.3.0",
40
40
  "eslint-plugin-import": "^2.29.1",
41
41
  "jest": "^29.7.0",
42
- "prettier": "^3.2.4",
42
+ "prettier": "^3.2.5",
43
43
  "supertest": "^6.3.4",
44
- "ts-jest": "^29.1.1",
44
+ "ts-jest": "^29.1.2",
45
45
  "ts-loader": "^9.5.1",
46
46
  "ts-node": "^10.9.2",
47
47
  "typescript": "^5.3.3"