-
Notifications
You must be signed in to change notification settings - Fork 84
[2135] Expose Dashboard client connection/disconnection state to Node-RED flows #2208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
n-lark
wants to merge
5
commits into
main
Choose a base branch
from
2135-client-presence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
09522c0
Add stable per-client identity and presence events (client-connect/re…
n-lark 15d0f60
Update docs for new clientId
n-lark 82aa5ab
Remove comment
n-lark 8329ff0
Merge remote-tracking branch 'origin/main' into 2135-client-presence
n-lark 94ec216
Update terms to connected and reconnected
n-lark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| const { EventEmitter } = require('events') | ||
|
|
||
| const DEFAULT_GRACE_MS = 20000 | ||
|
|
||
| /** | ||
| * Tracks client presence across reconnects (socket.id churns every reconnect; clientId is stable). | ||
| * A client may hold several live sockets (one per tab); events fire on its socket count crossing | ||
| * zero, not per socket. Emits { event, clientId, socketId? }: | ||
| * connected: count 0 -> 1, no grace pending (new client) | ||
| * reconnected: count 0 -> 1 during the grace window (returned before being declared gone) | ||
| * gone: count -> 0 and the grace window elapses | ||
| * Extra tabs (1 -> 2, 2 -> 1) emit nothing; the client is still present. | ||
| */ | ||
| function createClientStore ({ graceMs = DEFAULT_GRACE_MS, setTimeoutFn = setTimeout, clearTimeoutFn = clearTimeout } = {}) { | ||
| const clients = {} // clientId -> { sockets: Set<socketId>, graceTO } | ||
| const events = new EventEmitter() | ||
| events.setMaxListeners(0) | ||
|
|
||
| function connect (clientId, socketId) { | ||
| if (!clientId) { return } | ||
| let entry = clients[clientId] | ||
| const returning = !!(entry && entry.graceTO) | ||
| if (!entry) { | ||
| entry = clients[clientId] = { sockets: new Set(), graceTO: null } | ||
| } | ||
| if (entry.graceTO) { | ||
| clearTimeoutFn(entry.graceTO) | ||
| entry.graceTO = null | ||
| } | ||
| const wasEmpty = entry.sockets.size === 0 | ||
| entry.sockets.add(socketId) | ||
| if (wasEmpty) { | ||
| events.emit('client', { event: returning ? 'reconnected' : 'connected', clientId, socketId }) | ||
| } | ||
| } | ||
|
|
||
| function disconnect (clientId, socketId) { | ||
| if (!clientId) { return } | ||
| const entry = clients[clientId] | ||
| if (!entry) { return } | ||
| entry.sockets.delete(socketId) | ||
| if (entry.sockets.size === 0 && !entry.graceTO) { | ||
| entry.graceTO = setTimeoutFn(() => { | ||
| delete clients[clientId] | ||
| events.emit('client', { event: 'gone', clientId }) | ||
| }, graceMs) | ||
| } | ||
| } | ||
|
|
||
| return { connect, disconnect, events } | ||
| } | ||
|
|
||
| module.exports = { createClientStore } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| const should = require('should') // eslint-disable-line no-unused-vars | ||
|
|
||
| const { createClientStore } = require('../../nodes/store/clients.js') | ||
|
|
||
| // Controllable timer so grace behavior is deterministic | ||
| function fakeTimer () { | ||
| let pending = null | ||
| return { | ||
| setTimeoutFn: (fn) => { pending = fn; return { id: 1 } }, | ||
| clearTimeoutFn: () => { pending = null }, | ||
| fire: () => { const fn = pending; pending = null; if (fn) { fn() } } | ||
| } | ||
| } | ||
|
|
||
| function collect (store) { | ||
| const seen = [] | ||
| store.events.on('client', (e) => seen.push(e)) | ||
| return seen | ||
| } | ||
|
|
||
| describe('client store', function () { | ||
| it('emits connect on a genuinely new client', function () { | ||
| const store = createClientStore() | ||
| const events = collect(store) | ||
| store.connect('c1', 's1') | ||
| events.should.have.length(1) | ||
| events[0].should.match({ event: 'connected', clientId: 'c1' }) | ||
| }) | ||
|
|
||
| it('does not emit for a second tab of the same client', function () { | ||
| const store = createClientStore() | ||
| store.connect('c1', 's1') | ||
| const events = collect(store) | ||
| store.connect('c1', 's2') // second tab | ||
| events.should.have.length(0) | ||
| }) | ||
|
|
||
| it('does not go gone while one of two sockets is still live', function () { | ||
| const t = fakeTimer() | ||
| const store = createClientStore(t) | ||
| store.connect('c1', 's1') | ||
| store.connect('c1', 's2') | ||
| const events = collect(store) | ||
| store.disconnect('c1', 's1') // s2 still live -> no grace, no gone | ||
| t.fire() // nothing should be pending | ||
| events.should.have.length(0) | ||
| }) | ||
|
|
||
| it('emits reconnect (not gone) when it comes back within grace', function () { | ||
| const t = fakeTimer() | ||
| const store = createClientStore(t) | ||
| store.connect('c1', 's1') | ||
| const events = collect(store) | ||
| store.disconnect('c1', 's1') // last socket -> grace pending | ||
| store.connect('c1', 's2') // returns before grace fires | ||
| events.should.matchAny({ event: 'reconnected', clientId: 'c1' }) | ||
| events.should.not.matchAny({ event: 'gone' }) | ||
| }) | ||
|
|
||
| it('emits gone when the grace window elapses', function () { | ||
| const t = fakeTimer() | ||
| const store = createClientStore(t) | ||
| store.connect('c1', 's1') | ||
| const events = collect(store) | ||
| store.disconnect('c1', 's1') | ||
| t.fire() // grace elapses | ||
| events.should.matchAny({ event: 'gone', clientId: 'c1' }) | ||
| }) | ||
|
|
||
| it('re-emits connect after a client has gone (entry was deleted)', function () { | ||
| const t = fakeTimer() | ||
| const store = createClientStore(t) | ||
| store.connect('c1', 's1') | ||
| store.disconnect('c1', 's1') | ||
| t.fire() // gone -> entry deleted | ||
| const events = collect(store) | ||
| store.connect('c1', 's2') // same clientId, but it's a fresh presence now | ||
| events.should.matchAny({ event: 'connected', clientId: 'c1' }) | ||
| }) | ||
|
|
||
| it('is silent when a new socket arrives while the old one is still live', function () { | ||
| const store = createClientStore() | ||
| store.connect('c1', 's1') | ||
| const events = collect(store) | ||
| store.connect('c1', 's2') // e.g. reconnect races ahead of the old socket's drop | ||
| events.should.have.length(0) // looks like a second tab; no reconnect/connect | ||
| }) | ||
|
|
||
| it('ignores connects/disconnects with no clientId', function () { | ||
| const store = createClientStore() | ||
| const events = collect(store) | ||
| store.connect(undefined, 's1') | ||
| store.disconnect(undefined, 's1') | ||
| events.should.have.length(0) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const should = require('should') // eslint-disable-line no-unused-vars | ||
|
|
||
| const { getOrCreateClientId } = require('../../ui/src/util/client-id.js') | ||
|
|
||
| function fakeStorage () { | ||
| const map = {} | ||
| return { | ||
| getItem: (k) => (k in map ? map[k] : null), | ||
| setItem: (k, v) => { map[k] = v } | ||
| } | ||
| } | ||
|
|
||
| describe('getOrCreateClientId', function () { | ||
| it('generates and persists an id on first call', function () { | ||
| const storage = fakeStorage() | ||
| const id = getOrCreateClientId(storage, () => 'fixed-id') | ||
| id.should.equal('fixed-id') | ||
| storage.getItem('nrdb-client-id').should.equal('fixed-id') | ||
| }) | ||
|
|
||
| it('returns the same id on subsequent calls', function () { | ||
| const storage = fakeStorage() | ||
| const first = getOrCreateClientId(storage) | ||
| const second = getOrCreateClientId(storage) | ||
| second.should.equal(first) | ||
| }) | ||
|
|
||
| it('falls back to a fresh id when storage throws (blocked storage)', function () { | ||
| const blocked = { | ||
| getItem: () => { throw new Error('blocked') }, | ||
| setItem: () => { throw new Error('blocked') } | ||
| } | ||
| getOrCreateClientId(blocked, () => 'session-id').should.equal('session-id') | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if this is ideal behaviour.
One of the reasons to expose socketId at all is to be able to direct a message to just that dashboard.
I may open the same dashboard in two tabs, and want to treat them as separate 'sessions'. Each of those sessions would be identifiable via
clientId/socketIdpair - but only if I know about them.If we don't emit the client connected events for a second connection, we lose the ability to track all of the open dashboards.
Could we instead emit the events with an additional
socketsproperty that is an array of all active sockets we see this clientId on?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yahhh so I think you can accomplish that here by combining the socket level events with the client-presence events. The socket
connect/lostfire per tab and already come through with theclientId, so you can reconcile from that end and get the data you need.The
client-connected/reconnected/goneevents are meant to be client-level presence with the grace window, not per-tab. So if you wanted to completely manage both you could use both channels, totally open to adding a sockets array to the presence events if it's useful, but firing one of these on every socketconnect/disconnectfeels like it'd just duplicate the socket events.