angelia.io 2.4.70 → 2.4.73

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angelia.io",
3
- "version": "2.4.70",
3
+ "version": "2.4.73",
4
4
  "description": "WebSockets Server and Client API for node.js and the browser, with rooms support.",
5
5
  "homepage": "https://github.com/titoBouzout/angelia.io",
6
6
  "repository": {
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # angelia.io
2
2
 
3
- WebSockets Server and Client API for node.js and the browser, with rooms support.
3
+ WebSockets Server and Client API for node.js and the browser, (with rooms support in development).
4
4
 
5
5
  The goal of this project is to provide a developer friendly API that just works™.
6
6
 
@@ -12,18 +12,24 @@ If you fancy for Client side a regular JavaScript file, then use https://github.
12
12
 
13
13
  ## Simple Example
14
14
 
15
- ### Server (Node.js)
16
-
17
15
  ```JavaScript
18
16
  // server.js (node.js)
19
17
  import Server from 'angelia.io/server';
20
18
 
21
- class Connection {
22
- async listen() {
23
- console.log('-'.repeat(60));
24
- console.log('Server started listening on port ' + this.server.port);
25
- console.log(this.server);
19
+ class FancyChat {
20
+ async typing(socket, data, callback) {
21
+ console.log('Im', data ? ' typing' : ' not typing')
26
22
  }
23
+ async theMessage(socket, data, callback) {
24
+ console.log('the message is', data, socket)
25
+ socket.emit('gotIt', 'thanks')
26
+ callback('yes Im sure')
27
+ }
28
+ }
29
+
30
+ Server.on(FancyChat);
31
+
32
+ class Connection {
27
33
  async connect(socket, request) {
28
34
  console.log('socket connected', socket)
29
35
  }
@@ -33,42 +39,30 @@ class Connection {
33
39
  }
34
40
  Server.on(Connection);
35
41
 
36
- class FancyChat {
37
- async chatMessage(socket, message, callback) {
38
- console.log('socket sent', message)
39
- socket.emit('chatMessage', 'Hi client nice to meet you')
40
- callback('yes, got it')
41
- }
42
- }
43
- Server.on(FancyChat);
44
-
45
- const server = new Server.listen({
42
+ Server.listen({
46
43
  port: 3001,
47
44
  });
48
- ```
49
45
 
50
- ### Client (Browser)
51
-
52
- ```JavaScript
53
46
  // index.js (browser)
54
47
  import Client from 'angelia.io/client';
55
48
 
56
- const socket = new Client({
57
- url: 'ws://localhost:3001',
58
- });
49
+ const socket = new Client('ws://localhost:3001');
59
50
 
60
- socket.on('chatMessage', (message) => {
61
- console.log('Server sent', message)
62
- })
51
+ socket.emit('typing', true)
63
52
 
64
- socket.emit('chatMessage', 'Hi there server, Im client', (data) =>{
65
- console.log('you got it?', data)
53
+ setTimeout(() => {
54
+ socket.emit('typing', false)
55
+ socket.emit('theMessage', 'hi there!', (data) =>{
56
+ console.log('you sure?', data)
57
+ })
58
+ }, 10000)
59
+
60
+ socket.on('gotIt', (message) => {
61
+ console.log('Server got it yey', message)
66
62
  })
67
63
  ```
68
64
 
69
- ## Server (Node.js)
70
-
71
- ### Options
65
+ ## Server Documentation (Node.js)
72
66
 
73
67
  A call to `Server.listen` starts the server. `Server` is a singleton and can only have running 1 server.
74
68
 
@@ -76,7 +70,7 @@ A call to `Server.listen` starts the server. `Server` is a singleton and can onl
76
70
  // server.js (node.js)
77
71
  import Server from 'angelia.io/server'
78
72
 
79
- const server = new Server.listen({
73
+ Server.listen({
80
74
  hostname: 'localhost',
81
75
  port: 3001,
82
76
  maxMessageSize: 5,
@@ -85,15 +79,17 @@ const server = new Server.listen({
85
79
  })
86
80
  ```
87
81
 
88
- | name | kind | default | description |
89
- | -------------------- | ------- | ------- | --------------------------------------------------- |
90
- | `hostname` | String | '' | the hostname if any |
91
- | `port` | Number | 3001 | the port to use for this server |
92
- | `maxMessageSize` | Number | 5 | max size in mb of a message received |
93
- | `skipUTF8Validation` | Boolean | false | allows to skip utf8 validation |
94
- | `dontConnect` | Boolean | false | allows to instance the socket without connecting |
95
- | `cert` | String | '' | path to the cert file for using https fullchain.pem |
96
- | `key` | String | '' | path to the key file for using https privkey.pem |
82
+ ### `Server` Options
83
+
84
+ | name | kind | default | description |
85
+ | -------------------- | ----------- | ------- | ------------------------------------------------------------------------------------------------------------ |
86
+ | `hostname` | String | '' | the hostname if any |
87
+ | `port` | Number | 3001 | the port to use for this server |
88
+ | `maxMessageSize` | Number | 5 | max size in mb of a message received |
89
+ | `skipUTF8Validation` | Boolean | false | allows to skip utf8 validation |
90
+ | `cert` | String | '' | path to the cert file for using https fullchain.pem |
91
+ | `key` | String | '' | path to the key file for using https privkey.pem |
92
+ | `http` | node server | null | in case you want to use your own server. Else one will be created, as in `require('http(s)').createServer()` |
97
93
 
98
94
  ### Server Object
99
95
 
@@ -110,38 +106,46 @@ class _ {
110
106
  }
111
107
  Server.on(_)
112
108
 
113
- const server = new Server.listen({
109
+ Server.listen({
114
110
  port: 3001,
115
111
  })
116
112
  ```
117
113
 
118
- #### Server Properties
119
-
120
- | signature | kind | description |
121
- | ------------------------------------- | -------- | ---------------------------------------------------------------------------------------------- |
122
- | `since` | Number | timestamp of initialization |
123
- | `port` | Number | port used by this server |
124
- | `maxMessageSize` | Number | maximum message size in mb |
125
- | `timeout` | Number | after how long the socket is considered gone, in ms |
126
- | `connections` | Number | count of sockets connected |
127
- | `served` | Number | count of sockets ever connected |
128
- | `bytesSent` | Number | sum of bytes sent by the server |
129
- | `bytesReceived` | Number | sum of bytes the server has ever received |
130
- | `messagesSent` | Number | count of messages ever sent |
131
- | `messagesReceived` | Number | count of messages ever received |
132
- | `events` | Object | ref to events, ex: server.events.typing() to dispatch typing to anyone listening to it |
133
- | `listeners` | Array | for debbuging: array of listeners as strings |
134
- | `on(Class)` | Function | attaches all methods of a `Class` as listeners |
135
- | `on(Function)` | Function | attaches a named `Function` as a listener |
136
- | `on(Object)` | Function | attaches all properties of an object that are of the type `Function` as listeners |
137
- | `on(key, Function)` | Function | attaches a `Function` as a listener for `key` |
138
- | `emit(key, [value])` | Function | emits to all connected sockets |
139
- | `once(key, [value])` | Function | emits to the sockets and replace if exists a pending message with the same `key` |
140
- | `broadcast(sender, key, [value])` | Function | emits to all connected sockets except sender |
141
- | `broadcastOnce(sender, key, [value])` | Function | emits to the sockets except sender and replace if exists a pending message with the same `key` |
142
- | `sockets` | Set | a Set() with all the current connected sockets |
143
-
144
- ### Socket Object
114
+ #### List of `Server` Object Properties
115
+
116
+ <details>
117
+ <summary><b>Expand</b></summary>
118
+
119
+ | signature | kind | description |
120
+ | ------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------- |
121
+ | `since` | Number | timestamp of initialization |
122
+ | `now` | Number | timestamp that updates every half a second |
123
+ | `port` | Number | port used by this server |
124
+ | `hostname` | String | the hostname if any |
125
+ | `maxMessageSize` | Number | maximum message size in mb |
126
+ | `timeout` | Number | after how long the socket is considered gone, in ms |
127
+ | `connections` | Number | count of sockets connected |
128
+ | `served` | Number | count of sockets ever connected |
129
+ | `bytesSent` | Number | sum of bytes sent by the server |
130
+ | `bytesReceived` | Number | sum of bytes the server has ever received |
131
+ | `messagesSent` | Number | count of messages ever sent |
132
+ | `messagesReceived` | Number | count of messages ever received |
133
+ | `events` | Object | ref to events, ex: server.events.typing() to dispatch typing to anyone listening to it |
134
+ | `listeners` | Array | for debbuging: array of listeners as strings |
135
+ | `on(Class)` | Function | attaches all methods of a `Class` as listeners |
136
+ | `on(Function)` | Function | attaches a named `Function` as a listener |
137
+ | `on(Object)` | Function | attaches all properties of an object that are of the type `Function` as listeners |
138
+ | `on(key, Function)` | Function | attaches a `Function` as a listener for `key` |
139
+ | `emit(key, [value])` | Function | emits to all connected sockets |
140
+ | `once(key, [value])` | Function | emits to the sockets and replace if exists a pending message with the same `key` |
141
+ | `broadcast(sender, key, [value])` | Function | emits to all connected sockets except sender |
142
+ | `broadcastOnce(sender, key, [value])` | Function | emits to the sockets except sender and replace if exists a pending message with the same `key` |
143
+ | `sockets` | Set | a Set() with all the current connected sockets |
144
+ | `http` | node server | the underlying http(s) server |
145
+
146
+ </details>
147
+
148
+ ### `Socket` Object
145
149
 
146
150
  The `socket` object is given to you by a listener
147
151
 
@@ -151,17 +155,20 @@ import Server from 'angelia.io/server'
151
155
 
152
156
  class _ {
153
157
  connect(socket, request) {
154
- console.log(socket, data, callback)
158
+ console.log(socket, request)
155
159
  }
156
160
  }
157
161
  Server.on(_)
158
162
 
159
- new Server.listen({
163
+ Server.listen({
160
164
  port: 3001,
161
165
  })
162
166
  ```
163
167
 
164
- #### Socket Properties
168
+ #### List of `Socket` Object Properties
169
+
170
+ <details>
171
+ <summary><b>Expand</b></summary>
165
172
 
166
173
  | signature | kind | description |
167
174
  | --------------------------- | -------- | ----------------------------------------------------------------------------- |
@@ -182,72 +189,23 @@ new Server.listen({
182
189
  | `once(key, [value])` | Function | replace if exists a pending message with the same `key` from emit queue |
183
190
  | `disconnect([noReconnect])` | Function | disconnects the socket from the server, pass `true` to prevent re-connections |
184
191
 
192
+ </details>
193
+
185
194
  ### Listeners
186
195
 
187
196
  To listen for a client message/event you may do the familiar way `Server.on('connect', (socket) => {console.log('socket connected!', socket)})`
188
197
 
189
198
  However, to ease organization and development you may listen to an event by creating a class with any name, and give to methods the name of the things you want to listen to. You then add your class to the listeners as `Server.on(MyClass);` and you are done.
190
199
 
191
- On user defined listeners, the listener receives three things as sent by the client: `socket`, `data` and a `callback`; Example `class FancyChat { async typing(socket, data, callback) {console.log(socket, data);callback('got it')}}`.
192
-
193
- If you are building a chat you may write something like this
194
-
195
- ```JavaScript
196
- // server.js (node.js)
197
- import Server from 'angelia.io/server';
198
-
199
- class FancyChat {
200
- async typing(socket, data, callback) {
201
- console.log('Im', data ? ' typing' : ' not typing')
202
- }
203
- async theMessage(socket, data, callback) {
204
- console.log('the message is', data, socket)
205
- socket.emit('gotIt', 'thanks')
206
- callback('yes Im sure')
207
- }
208
- }
209
-
210
- Server.on(FancyChat);
211
-
212
- class Connection {
213
- async connect(socket, request) {
214
- console.log('socket connected', socket)
215
- }
216
- async disconnect(socket, code, message) {
217
- console.log('socket disconnected', code, message, socket);
218
- }
219
- }
220
- Server.on(Connection);
221
-
222
- const server = new Server.listen({
223
- port: 3001,
224
- });
225
-
226
- // index.js (browser)
227
- import Client from 'angelia.io/client';
228
-
229
- const socket = new Client({
230
- url: 'ws://localhost:3001',
231
- });
232
-
233
- socket.emit('typing', true)
234
-
235
- setTimeout(() => {
236
- socket.emit('typing', false)
237
- socket.emit('theMessage', 'hi there!', (data) =>{
238
- console.log('you sure?', data)
239
- })
240
- }, 10000)
241
-
242
- socket.on('gotIt', (message) => {
243
- console.log('Server got it yey', message)
244
- })
245
- ```
200
+ On user defined listeners, the listener receives three things as sent by the client: `socket`, `data` and a `callback`; Example `class FancyChat { async typing(socket, data, callback) {console.log(socket, data);}}`.
246
201
 
247
202
  #### Syntax For Listeners
248
203
 
249
204
  Listeners have the following alternative syntax if you feel like
250
205
 
206
+ <details>
207
+ <summary><b>Expand</b></summary>
208
+
251
209
  ```javascript
252
210
  // server.js (node.js)
253
211
  import Server from 'angelia.io/server'
@@ -290,12 +248,14 @@ Server.on('connect', function fancyConnect(socket, request) {
290
248
  onsole.log('connect in named function')
291
249
  })
292
250
 
293
- const server = new Server.listen({
251
+ Server.listen({
294
252
  port: 3001,
295
253
  })
296
254
  ```
297
255
 
298
- ### Predefined Events
256
+ </details>
257
+
258
+ ### Predefined `Server` Events
299
259
 
300
260
  There's a bunch of handy predefined events dispatched whenever you add listeners for them.
301
261
 
@@ -315,12 +275,15 @@ class _ {
315
275
 
316
276
  Server.on(_);
317
277
 
318
- const server = new Server.listen({
278
+ Server.listen({
319
279
  port: 3001,
320
280
  });
321
281
  ```
322
282
 
323
- #### List of Predefined Events
283
+ #### List of Predefined `Server` Events
284
+
285
+ <details>
286
+ <summary><b>Expand</b></summary>
324
287
 
325
288
  | signature | description |
326
289
  | ----------------------------------- | ----------------------------------------------------------------------------------------- |
@@ -341,16 +304,20 @@ const server = new Server.listen({
341
304
  | `events` | reference to event dispatcher, ex: `this.events.typing()` will dispatch the `typing` event to anyone listening to it |
342
305
  | `classes` | reference to all functions that have been attached as listeners , ex: `this.classes.MyFancyChat.typing` |
343
306
 
307
+ </details>
308
+
344
309
  ## Client API (Browser)
345
310
 
346
- ### Options
311
+ ### `Client` Options
347
312
 
348
313
  Configurable options used by the constructor
349
314
 
350
315
  ```javascript
351
316
  const socket = new Client({
352
317
  url: 'ws://localhost:3001',
353
- params: function (){ return { fast: 'data', test: 'a space' })
318
+ params: function () {
319
+ return { fast: 'data', test: 'a space' }
320
+ },
354
321
  })
355
322
  ```
356
323
 
@@ -359,6 +326,7 @@ const socket = new Client({
359
326
  | `url` | string | 'ws(s)://\${window.location.hostname}:3001' | url of the socket server, example 'ws://localhost:3001' |
360
327
  | `params` | Function | {} | to send data while connecting, accesible via `socket.params` server side |
361
328
  | `longLiveFlash` | boolean | false | browsers throw when calling swf functions via ExternalInterface after events like WebSocket.onmesssage; setting this to `true` fix it by dispatching them in a setTimeout |
329
+ | `dontConnect` | Boolean | false | allows to instance the socket without connecting |
362
330
 
363
331
  You may also do like this if you don't need any option
364
332
 
@@ -366,10 +334,13 @@ You may also do like this if you don't need any option
366
334
  const socket = new Client('ws://localhost:3001')
367
335
  ```
368
336
 
369
- ### API
337
+ ### `Client` API
370
338
 
371
339
  The client API is similar to regular event handling
372
340
 
341
+ <details>
342
+ <summary><b>Expand</b></summary>
343
+
373
344
  | signature | kind | description |
374
345
  | ------------------------------ | -------- | ------------------------------------------------------------------ |
375
346
  | `connected` | Boolean | `true` when the socket is connected else `false` |
@@ -380,16 +351,23 @@ The client API is similar to regular event handling
380
351
  | `emit(key, [value, callback])` | Function | emits data to the server |
381
352
  | `decode(data)` | Function | for decoding binary data, returns a promise |
382
353
 
383
- #### List of Predefined Events
354
+ </details>
355
+
356
+ #### List of Predefined `Client` Events
384
357
 
385
358
  As in `socket.on('connect', () => console.log('connect happened!'))`
386
359
 
360
+ <details>
361
+ <summary><b>Expand</b></summary>
362
+
387
363
  | signature | description |
388
364
  | ------------ | ------------------------------------------------------------------------------------------------------- |
389
365
  | `connect` | **this happens only once** when we connect to the server, any future connection is a `reconnect` |
390
366
  | `reconnect` | if we were connected at least once, then any reconnection will dispatch this event instead of `connect` |
391
367
  | `disconnect` | when we disconnect from the server |
392
368
 
369
+ </details>
370
+
393
371
  ## Authors
394
372
 
395
373
  - Tito Bouzout https://github.com/titoBouzout
package/server/Server.js CHANGED
@@ -17,6 +17,7 @@ class ServerSingleton {
17
17
 
18
18
  cert: '',
19
19
  key: '',
20
+ http: null,
20
21
 
21
22
  queue: [],
22
23
 
@@ -92,6 +93,7 @@ class ServerSingleton {
92
93
  maxMessageSize: +options.maxMessageSize > 0 ? +options.maxMessageSize : this.maxMessageSize,
93
94
  cert: options.cert || this.cert,
94
95
  key: options.key || this.key,
96
+ http: options.http || this.http,
95
97
 
96
98
  since: Date.now(),
97
99
  now: Date.now(),
@@ -104,26 +106,27 @@ class ServerSingleton {
104
106
  setInterval(this.updateNow, 500)
105
107
 
106
108
  // fires the server
107
- let server
108
- function handle(req, res) {
109
- res.writeHead(200, { 'Content-Type': 'text/plain' })
110
- res.end()
111
- }
112
- if (this.cert && this.key) {
113
- let fs = require('fs')
114
- server = require('https').createServer(
115
- {
116
- cert: fs.readFileSync(this.cert), // fullchain.pem
117
- key: fs.readFileSync(this.key), // privkey.pem
118
- },
119
- handle,
120
- )
121
- } else {
122
- server = require('http').createServer(handle)
109
+ if (!this.http) {
110
+ function handle(req, res) {
111
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
112
+ res.end()
113
+ }
114
+ if (this.cert && this.key) {
115
+ let fs = require('fs')
116
+ this.http = require('https').createServer(
117
+ {
118
+ cert: fs.readFileSync(this.cert), // fullchain.pem
119
+ key: fs.readFileSync(this.key), // privkey.pem
120
+ },
121
+ handle,
122
+ )
123
+ } else {
124
+ this.http = require('http').createServer(handle)
125
+ }
123
126
  }
124
127
 
125
128
  let io = new this.WebSocket.Server({
126
- server: server,
129
+ server: this.http,
127
130
  perMessageDeflate: false,
128
131
  maxPayload: this.maxMessageSize * 1024 * 1024,
129
132
  clientTracking: false,
@@ -135,7 +138,7 @@ class ServerSingleton {
135
138
  io.on('connection', this.onconnect)
136
139
  io.on('error', this.onerror)
137
140
 
138
- server.listen(this.port, this.hostname || null)
141
+ this.http.listen(this.port, this.hostname || null)
139
142
 
140
143
  for (let m of ['RESTART', 'SIGINT', 'SIGTERM', 'SIGBREAK']) {
141
144
  process.on(m, () => {
@@ -156,8 +159,6 @@ class ServerSingleton {
156
159
 
157
160
  console.log('Socket Server Started Listening On Port ', this.port)
158
161
  this.events.listen && this.events.listen()
159
-
160
- return this
161
162
  }
162
163
 
163
164
  // count of connections
@@ -9,6 +9,10 @@ class Room {
9
9
  has(socket) {
10
10
  return this.sockets.has(socket)
11
11
  }
12
+ get size() {
13
+ return this.sockets.size
14
+ }
15
+ // private
12
16
  [join](roomList, socket) {
13
17
  // add to list of room list
14
18
  if (!this[parent]) {
@@ -47,7 +51,7 @@ class Room {
47
51
  this[parent][remove](this)
48
52
 
49
53
  // dispatch leave
50
- this.onLeave && this.onLeave(socket)
54
+ // this.onLeave && this.onLeave(socket)
51
55
 
52
56
  // dispatch that the room has been deleted
53
57
  this.onDelete && this.onDelete(socket)
@@ -14,7 +14,9 @@ class Rooms {
14
14
  get(id) {
15
15
  return this.roomsById.get(id)
16
16
  }
17
-
17
+ get size() {
18
+ return this.rooms.size
19
+ }
18
20
  // private
19
21
  [add](room) {
20
22
  if (!this.rooms.has(room)) {