Skip to content

Commit 8bdbcd8

Browse files
committed
feat: Add connectionsCount and documentsCount methods to server
1 parent fc9bec4 commit 8bdbcd8

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

docs/src/docPages/api/methods.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Our goal: Let’s keep it simple. The server has a few methods only.
1111
| `listen()` | Start the server. |
1212
| `configure(configuration)` | Pass custom settings. |
1313
| `handleConnection(incoming, request, documentName, context)` | Bind the server to an existing server instance. |
14+
| `documentsCount()` | Get the total number of active documents |
15+
| `connectionsCount()` | Get the total number of active connections |
1416
| `closeConnections(documentName?)` | Close all connections, or to a specific document. |
1517
| `destroy()` | Stop the server. |
1618

packages/server/src/Document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Document extends Doc {
104104
}
105105

106106
/**
107-
* Get the number of active connections
107+
* Get the number of active connections for this document
108108
*/
109109
connectionsCount(): number {
110110
return this.connections.size

packages/server/src/Hocuspocus.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ export class Hocuspocus {
138138
})
139139
}
140140

141+
/**
142+
* Get the total number of active documents
143+
*/
144+
documentsCount(): number {
145+
return this.documents.size
146+
}
147+
148+
/**
149+
* Get the total number of active connections
150+
*/
151+
connectionsCount(): number {
152+
return Array.from(this.documents.values()).reduce((acc, document) => {
153+
acc += document.connectionsCount()
154+
return acc
155+
}, 0)
156+
}
157+
141158
/**
142159
* Force close one or more connections
143160
*/

tests/server/connectionsCount.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// import assert from 'assert'
2+
import * as Y from 'yjs'
3+
import WebSocket from 'ws'
4+
import { assert } from 'chai'
5+
import { Hocuspocus } from '../../packages/server/src'
6+
import { HocuspocusProvider } from '../../packages/provider/src'
7+
8+
const ydoc = new Y.Doc()
9+
10+
context('server/connectionsCount', () => {
11+
it('outputs the total connections', done => {
12+
const Server = new Hocuspocus()
13+
14+
Server.configure({
15+
port: 4000,
16+
})
17+
18+
Server.listen()
19+
20+
const client = new HocuspocusProvider({
21+
url: 'ws://127.0.0.1:4000',
22+
name: 'hocuspocus-test',
23+
document: ydoc,
24+
WebSocketPolyfill: WebSocket,
25+
onSynced() {
26+
assert.strictEqual(Server.connectionsCount(), 1)
27+
28+
const client2 = new HocuspocusProvider({
29+
url: 'ws://127.0.0.1:4000',
30+
name: 'hocuspocus-test2',
31+
document: ydoc,
32+
WebSocketPolyfill: WebSocket,
33+
onSynced() {
34+
assert.strictEqual(Server.connectionsCount(), 2)
35+
36+
client2.destroy()
37+
client.destroy()
38+
Server.destroy()
39+
done()
40+
},
41+
})
42+
},
43+
})
44+
})
45+
})

tests/server/documentsCount.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// import assert from 'assert'
2+
import * as Y from 'yjs'
3+
import WebSocket from 'ws'
4+
import { assert } from 'chai'
5+
import { Hocuspocus } from '../../packages/server/src'
6+
import { HocuspocusProvider } from '../../packages/provider/src'
7+
8+
const ydoc = new Y.Doc()
9+
10+
context('server/documentsCount', () => {
11+
it('outputs the total active documents', done => {
12+
const Server = new Hocuspocus()
13+
14+
Server.configure({
15+
port: 4000,
16+
})
17+
18+
Server.listen()
19+
20+
const client = new HocuspocusProvider({
21+
url: 'ws://127.0.0.1:4000',
22+
name: 'hocuspocus-test',
23+
document: ydoc,
24+
WebSocketPolyfill: WebSocket,
25+
onSynced() {
26+
assert.strictEqual(Server.documentsCount(), 1)
27+
28+
client.destroy()
29+
Server.destroy()
30+
done()
31+
},
32+
})
33+
})
34+
})

0 commit comments

Comments
 (0)