Skip to content
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

Fix missing presence events on subscription #419

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6403,6 +6403,7 @@
* @param event - Received real-time event.
*/
emitEvent(event) {
var _a;
if (event.type === PubNubEventType.Message) {
this.listenerManager.announceMessage(event.data);
this.announce('message', event.data, event.data.channel, event.data.subscription);
Expand All @@ -6413,7 +6414,7 @@
}
else if (event.type === PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, (_a = event.data.subscription) !== null && _a !== void 0 ? _a : event.data.channel, event.data.subscription);
}
else if (event.type === PubNubEventType.AppContext) {
const { data: objectEvent } = event;
Expand Down Expand Up @@ -9963,7 +9964,7 @@
* types of events.
*/
addListener(listener) {
this.eventEmitter.addListener(listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
}
/**
* Remove events handler.
Expand Down Expand Up @@ -10065,7 +10066,7 @@
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Add additional entity's subscription to the subscription set.
Expand Down Expand Up @@ -10187,7 +10188,7 @@
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Merge entities' subscription objects into subscription set.
Expand Down Expand Up @@ -10283,9 +10284,12 @@
*/
subscription(subscriptionOptions) {
{
const channelGroups = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);
return new Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down Expand Up @@ -10370,8 +10374,11 @@
*/
subscription(subscriptionOptions) {
{
const channels = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);
return new Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/core/components/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EventEmitter {
* @param event - Received real-time event.
*/
emitEvent(event) {
var _a;
if (event.type === subscribe_1.PubNubEventType.Message) {
this.listenerManager.announceMessage(event.data);
this.announce('message', event.data, event.data.channel, event.data.subscription);
Expand All @@ -55,7 +56,7 @@ class EventEmitter {
}
else if (event.type === subscribe_1.PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, (_a = event.data.subscription) !== null && _a !== void 0 ? _a : event.data.channel, event.data.subscription);
}
else if (event.type === subscribe_1.PubNubEventType.AppContext) {
const { data: objectEvent } = event;
Expand Down
5 changes: 4 additions & 1 deletion lib/entities/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class Channel {
*/
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channels = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);
return new Subscription_1.Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
5 changes: 4 additions & 1 deletion lib/entities/ChannelGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ class ChannelGroup {
*/
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channelGroups = [this.name];
if ((subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);
return new Subscription_1.Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/SubscribeCapable.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SubscribeCapable {
* types of events.
*/
addListener(listener) {
this.eventEmitter.addListener(listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
}
/**
* Remove events handler.
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Subscription extends SubscribeCapable_1.SubscribeCapable {
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Merge entities' subscription objects into subscription set.
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/SubscriptionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SubscriptionSet extends SubscribeCapable_1.SubscribeCapable {
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(this.listener, this.channelNames.filter((c) => !c.endsWith('-pnpres')), this.groupNames.filter((cg) => !cg.endsWith('-pnpres')));
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}
/**
* Add additional entity's subscription to the subscription set.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/components/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class EventEmitter {
this.announce('signal', event.data, event.data.channel, event.data.subscription);
} else if (event.type === PubNubEventType.Presence) {
this.listenerManager.announcePresence(event.data);
this.announce('presence', event.data, event.data.channel, event.data.subscription);
this.announce('presence', event.data, event.data.subscription ?? event.data.channel, event.data.subscription);
} else if (event.type === PubNubEventType.AppContext) {
const { data: objectEvent } = event;
const { message: object } = objectEvent;
Expand Down
6 changes: 5 additions & 1 deletion src/entities/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ export class Channel {
*/
subscription(subscriptionOptions?: SubscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channels = [this.name];
if (subscriptionOptions?.receivePresenceEvents && !this.name.endsWith('-pnpres'))
channels.push(`${this.name}-pnpres`);

return new Subscription({
channels: subscriptionOptions?.receivePresenceEvents ? [this.name, `${this.name}-pnpres`] : [this.name],
channels,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
Expand Down
6 changes: 5 additions & 1 deletion src/entities/ChannelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ export class ChannelGroup {
*/
subscription(subscriptionOptions?: SubscriptionOptions) {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const channelGroups = [this.name];
if (subscriptionOptions?.receivePresenceEvents && !this.name.endsWith('-pnpres'))
channelGroups.push(`${this.name}-pnpres`);

return new Subscription({
channels: [],
channelGroups: subscriptionOptions?.receivePresenceEvents ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups,
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
Expand Down
6 changes: 1 addition & 5 deletions src/entities/SubscribeCapable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ export abstract class SubscribeCapable {
* types of events.
*/
addListener(listener: Listener) {
this.eventEmitter.addListener(
listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
this.eventEmitter.addListener(listener, this.channelNames, this.groupNames);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/entities/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ export class Subscription extends SubscribeCapable {
this.pubnub = pubnub;
this.eventEmitter = eventEmitter;
this.listener = {};
eventEmitter.addListener(
this.listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/entities/SubscriptionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ export class SubscriptionSet extends SubscribeCapable {
this.subscriptionList.push(subscription);
});
this.listener = {};
eventEmitter.addListener(
this.listener,
this.channelNames.filter((c) => !c.endsWith('-pnpres')),
this.groupNames.filter((cg) => !cg.endsWith('-pnpres')),
);
eventEmitter.addListener(this.listener, this.channelNames, this.groupNames);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion test/integration/endpoints/fetch_messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,16 @@ describe('fetch messages endpoints', () => {
assert(errorCatched);
});

it.only('supports custom message type', (done) => {
it('supports custom message type', (done) => {
const channel = PubNub.generateUUID();
const expectedMessagesCount = 2;

publishMessagesToChannel(pubnub, expectedMessagesCount, channel, 'test-message-type', (messages) => {
const messageTimetokens = messages.map((message) => message.timetoken);

pubnub.fetchMessages({ channels: [channel], includeCustomMessageType: true }, (status, response) => {
assert.equal(status.error, false, `Fetch messages error: ${JSON.stringify(status.errorData)}`);

try {
assert.equal(status.error, false);
assert(response !== null);
Expand Down
30 changes: 16 additions & 14 deletions test/integration/endpoints/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('publish endpoints', () => {
});
});

it('supports customMessageType', (done) => {
it.only('supports customMessageType', (done) => {
const scope = utils
.createNock()
.get('/publish/myPublishKey/mySubKey/0/ch1/0/%7B%22such%22%3A%22object%22%7D')
Expand All @@ -219,22 +219,24 @@ describe('publish endpoints', () => {
uuid: 'myUUID',
auth: 'myAuthKey',
store: '0',
custom_message_type: 'test-message-type',
})
.reply(200, '[1,"Sent","14647523059145592"]', { 'content-type': 'text/javascript' });

pubnub.setCipherKey('myCipherKey');

pubnub.publish({ message: { such: 'object' }, channel: 'ch1', storeInHistory: false }, (status, response) => {
try {
assert.equal(status.error, false);
assert(response !== null);
assert.deepEqual(response.timetoken, '14647523059145592');
assert.equal(scope.isDone(), true);
done();
} catch (error) {
done(error);
}
});
pubnub.publish(
{ message: { such: 'object' }, channel: 'ch1', storeInHistory: false, customMessageType: 'test-message-type' },
(status, response) => {
try {
assert.equal(status.error, false, `Message publish error: ${JSON.stringify(status.errorData)}`);
assert(response !== null);
assert.deepEqual(response.timetoken, '14647523059145592');
assert.equal(scope.isDone(), true);
done();
} catch (error) {
done(error);
}
},
);
});

it('publishes a complex object via POST', (done) => {
Expand Down
Loading