mongo-realtime 1.0.2 → 1.0.3

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.
Files changed (4) hide show
  1. package/README.md +18 -20
  2. package/index.js +27 -0
  3. package/logo.png +0 -0
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A Node.js package that combines Socket.IO and MongoDB Change Streams to deliver real-time database updates to your WebSocket clients.
4
4
 
5
+ ![Banner](logo.png)
6
+
5
7
  ## 🚀 Features
6
8
 
7
9
  - **Real-time updates**: Automatically detects changes in MongoDB and broadcasts them via Socket.IO
@@ -69,6 +71,8 @@ Initializes the socket system and MongoDB Change Streams.
69
71
  |-----------|------|-------------|
70
72
  | `options.connection` | `mongoose.Connection`* | Active Mongoose connection |
71
73
  | `options.server` | `http.Server`* | HTTP server to attach Socket.IO |
74
+ | `options.authentify` | `Function` | Function to authenticate socket connections. Should return true if authenticated |
75
+ | `options.middlewares` | `Array[Function]` | Array of Socket.IO middlewares |
72
76
  | `options.onSocket` | `Function` | Callback on socket connection |
73
77
  | `options.offSocket` | `Function` | Callback on socket disconnection |
74
78
  | `options.watch` | `Array[String]` | Collections to only watch. Listen to all when is empty |
@@ -119,9 +123,7 @@ MongoRealtime.listen("db:insert:users",anotherAction2);
119
123
 
120
124
  ```js
121
125
  MongoRealtime.removeListener("db:insert:users",sendNotification); // remove this specific action from this event
122
-
123
126
  MongoRealtime.removeListener("db:insert:users"); // remove all actions from this event
124
-
125
127
  MongoRealtime.removeAllListeners(); // remove all listeners
126
128
 
127
129
  ```
@@ -219,29 +221,25 @@ MongoRealtime.init({
219
221
  ### Socket Authentication
220
222
 
221
223
  ```javascript
222
- function authenticateSocket(socket){
223
- socket.on('authenticate', (token) => {
224
- if (isValidToken(token)) {
225
- socket.authenticated = true;
226
- socket.emit('authenticated');
227
- } else {
228
- socket.disconnect();
229
- }
230
- });
231
-
232
- socket.use((packet, next) => {
233
- if (socket.authenticated) {
234
- next();
235
- } else {
236
- next(new Error('Unauthenticated'));
237
- }
238
- });
224
+ function authenticateSocket(token, socket){
225
+ const verify = AuthService.verifyToken(token);
226
+ if(verify){
227
+ socket.user = verify.user; // attach user info to socket
228
+ return true; // should return true to accept the connection
229
+ }
230
+ return false;
239
231
  }
240
232
 
241
233
  MongoRealtime.init({
242
234
  connection: mongoose.connection,
243
235
  server: server,
244
- onSocket: authenticateSocket,
236
+ authentify: authenticateSocket,
237
+ middlewares: [
238
+ (socket, next) => {
239
+ console.log(`User is authenticated: ${socket.user.email}`);
240
+ next();
241
+ }
242
+ ],
245
243
  offSocket: (socket, reason) => {
246
244
  console.log(`Socket ${socket.id} disconnected: ${reason}`);
247
245
  }
package/index.js CHANGED
@@ -12,6 +12,8 @@ class MongoRealtime {
12
12
  *
13
13
  * @param {Object} options
14
14
  * @param {import("mongoose").Connection} options.connection - Active Mongoose connection
15
+ * @param {(token:String, socket: import("socket.io").Socket) => boolean | Promise<boolean>} options.authentify - Auth function that should return true if `token` is valid
16
+ * @param {[( socket: import("socket.io").Socket, next: (err?: ExtendedError) => void) => void]} options.middlewares - Register mmiddlewares on incoming socket
15
17
  * @param {(socket: import("socket.io").Socket) => void} options.onSocket - Callback triggered when a socket connects
16
18
  * @param {(socket: import("socket.io").Socket, reason: import("socket.io").DisconnectReason) => void} options.offSocket - Callback triggered when a socket disconnects
17
19
  * @param {import("http").Server} options.server - HTTP server to attach Socket.IO to
@@ -22,6 +24,8 @@ class MongoRealtime {
22
24
  static init({
23
25
  connection,
24
26
  server,
27
+ authentify,
28
+ middlewares=[],
25
29
  onSocket,
26
30
  offSocket,
27
31
  watch = [],
@@ -37,6 +41,29 @@ class MongoRealtime {
37
41
  watch = watch.map((s) => s.toLowerCase());
38
42
  ignore = ignore.map((s) => s.toLowerCase());
39
43
 
44
+ this.io.use(async (socket, next) => {
45
+ if (!!authentify) {
46
+ try {
47
+ const token = socket.handshake.auth.token;
48
+ if (!token) return next(new Error("No token provided"));
49
+
50
+ const authorized =await authentify(token, socket);
51
+ if (authorized===true) return next(); // exactly returns true
52
+
53
+ return next(new Error("Unauthorized"));
54
+ } catch (error) {
55
+ return next(new Error("Authentication error"));
56
+ }
57
+ } else {
58
+ return next();
59
+ }
60
+
61
+ });
62
+
63
+ for (let middleware of middlewares) {
64
+ this.io.use(middleware);
65
+ }
66
+
40
67
  this.io.on("connection", (socket) => {
41
68
  this.sockets = [...this.io.sockets.sockets.values()];
42
69
  if (onSocket) onSocket(socket);
package/logo.png ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-realtime",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "scripts": {},
6
6
  "keywords": [