Skip to content

Commit

Permalink
feat: Add connectionsCount and documentsCount methods to server
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed Sep 13, 2021
1 parent fc9bec4 commit 8bdbcd8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/src/docPages/api/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Our goal: Let’s keep it simple. The server has a few methods only.
| `listen()` | Start the server. |
| `configure(configuration)` | Pass custom settings. |
| `handleConnection(incoming, request, documentName, context)` | Bind the server to an existing server instance. |
| `documentsCount()` | Get the total number of active documents |
| `connectionsCount()` | Get the total number of active connections |
| `closeConnections(documentName?)` | Close all connections, or to a specific document. |
| `destroy()` | Stop the server. |

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Document extends Doc {
}

/**
* Get the number of active connections
* Get the number of active connections for this document
*/
connectionsCount(): number {
return this.connections.size
Expand Down
17 changes: 17 additions & 0 deletions packages/server/src/Hocuspocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ export class Hocuspocus {
})
}

/**
* Get the total number of active documents
*/
documentsCount(): number {
return this.documents.size
}

/**
* Get the total number of active connections
*/
connectionsCount(): number {
return Array.from(this.documents.values()).reduce((acc, document) => {
acc += document.connectionsCount()
return acc
}, 0)
}

/**
* Force close one or more connections
*/
Expand Down
45 changes: 45 additions & 0 deletions tests/server/connectionsCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// import assert from 'assert'
import * as Y from 'yjs'
import WebSocket from 'ws'
import { assert } from 'chai'
import { Hocuspocus } from '../../packages/server/src'
import { HocuspocusProvider } from '../../packages/provider/src'

const ydoc = new Y.Doc()

context('server/connectionsCount', () => {
it('outputs the total connections', done => {
const Server = new Hocuspocus()

Server.configure({
port: 4000,
})

Server.listen()

const client = new HocuspocusProvider({
url: 'ws://127.0.0.1:4000',
name: 'hocuspocus-test',
document: ydoc,
WebSocketPolyfill: WebSocket,
onSynced() {
assert.strictEqual(Server.connectionsCount(), 1)

const client2 = new HocuspocusProvider({
url: 'ws://127.0.0.1:4000',
name: 'hocuspocus-test2',
document: ydoc,
WebSocketPolyfill: WebSocket,
onSynced() {
assert.strictEqual(Server.connectionsCount(), 2)

client2.destroy()
client.destroy()
Server.destroy()
done()
},
})
},
})
})
})
34 changes: 34 additions & 0 deletions tests/server/documentsCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// import assert from 'assert'
import * as Y from 'yjs'
import WebSocket from 'ws'
import { assert } from 'chai'
import { Hocuspocus } from '../../packages/server/src'
import { HocuspocusProvider } from '../../packages/provider/src'

const ydoc = new Y.Doc()

context('server/documentsCount', () => {
it('outputs the total active documents', done => {
const Server = new Hocuspocus()

Server.configure({
port: 4000,
})

Server.listen()

const client = new HocuspocusProvider({
url: 'ws://127.0.0.1:4000',
name: 'hocuspocus-test',
document: ydoc,
WebSocketPolyfill: WebSocket,
onSynced() {
assert.strictEqual(Server.documentsCount(), 1)

client.destroy()
Server.destroy()
done()
},
})
})
})

0 comments on commit 8bdbcd8

Please sign in to comment.