Skip to content

Commit

Permalink
fix missing connInfo and chanInfo in presence (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Apr 1, 2023
1 parent af6209b commit 6a0610b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install dependencies
run: yarn install
- name: Start Centrifugo
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --client_insecure --http_stream --sse
run: docker run -d -p 8000:8000 -e CENTRIFUGO_PRESENCE=true centrifugo/centrifugo:latest centrifugo --client_insecure --http_stream --sse
- name: Lint
run: yarn lint
- name: Test
Expand Down
41 changes: 41 additions & 0 deletions src/centrifuge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,44 @@ test.each(transportCases.slice(0, 1))("%s: not connecting on online in disconnec
networkEventTarget.dispatchEvent(onlineEvent);
expect(c.state).toBe(Centrifuge.State.Disconnected);
});

test.each(transportCases)("%s: subscribe and presence", async (transport, endpoint) => {
const c = new Centrifuge([{
transport: transport as TransportName,
endpoint: endpoint,
}], {
websocket: WebSocket,
fetch: fetch,
eventsource: EventSource,
readableStream: ReadableStream,
emulationEndpoint: 'http://localhost:8000/emulation'
});

c.connect();
await c.ready(5000);
const sub = c.newSubscription('test');

sub.subscribe()
await sub.ready(5000);
expect(sub.state).toBe(Centrifuge.SubscriptionState.Subscribed);
expect(c.state).toBe(Centrifuge.State.Connected);

const presence = await sub.presence();
expect(Object.keys(presence.clients).length).toBeGreaterThan(0);

const presenceStats = await sub.presenceStats();
expect(presenceStats.numClients).toBeGreaterThan(0)
expect(presenceStats.numUsers).toBeGreaterThan(0);

let disconnectCalled: any;
const disconnectedPromise = new Promise<DisconnectedContext>((resolve, _) => {
disconnectCalled = resolve;
})
c.on('disconnected', (ctx) => {
disconnectCalled(ctx);
})

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
});
15 changes: 14 additions & 1 deletion src/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,21 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli

return this._methodCall().then(function () {
return self._callPromise(cmd, function (reply: any) {
const clients = reply.presence.presence;
for (const clientId in clients) {
if (clients.hasOwnProperty(clientId)) {
const connInfo = clients[clientId]['conn_info'];
const chanInfo = clients[clientId]['chan_info'];
if (connInfo) {
clients[clientId].connInfo = connInfo;
}
if (chanInfo) {
clients[clientId].chanInfo = chanInfo;
}
}
}
return {
'clients': reply.presence.presence
'clients': clients
};
});
});
Expand Down
39 changes: 39 additions & 0 deletions src/protobuf/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,42 @@ test.each(transportCases)("%s (Protobuf): publish and receive message", async (t
c.disconnect();
expect(ctx.data).toStrictEqual(binary);
});

test.each(transportCases)("%s (Protobuf): subscribe and presence", async (transport, endpoint) => {
const c = new Centrifuge([{
transport: transport as TransportName,
endpoint: endpoint,
}], {
protocol: 'protobuf',
websocket: WebSocket,
fetch: fetch,
readableStream: ReadableStream,
emulationEndpoint: 'http://localhost:8000/emulation'
});

c.connect();
await c.ready(5000);

const sub = c.newSubscription('test');
sub.subscribe()
await sub.ready(5000);

const presence = await sub.presence();
expect(Object.keys(presence.clients).length).toBeGreaterThan(0);

const presenceStats = await sub.presenceStats();
expect(presenceStats.numClients).toBeGreaterThan(0)
expect(presenceStats.numUsers).toBeGreaterThan(0);

let disconnectCalled: any;
const disconnectedPromise = new Promise<DisconnectedContext>((resolve, _) => {
disconnectCalled = resolve;
})
c.on('disconnected', (ctx) => {
disconnectCalled(ctx);
})

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
});

0 comments on commit 6a0610b

Please sign in to comment.