Skip to content

Commit

Permalink
Add custom message type support (#418)
Browse files Browse the repository at this point in the history
feat(message-type): add custom message type support

Add custom message type support for the following APIs: publish, signal, share file, subscribe
and history.
  • Loading branch information
parfeon authored Nov 14, 2024
1 parent 66520c2 commit 2b523b2
Show file tree
Hide file tree
Showing 27 changed files with 372 additions and 84 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2024-11-14
version: v8.3.0
changes:
- type: feature
text: "Add custom message type support for the following APIs: publish, signal, share file, subscribe and history."
- date: 2024-10-31
version: v8.2.10
changes:
Expand Down Expand Up @@ -1062,7 +1067,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '8.2.10'
version: '8.3.0'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -1078,7 +1083,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.2.10.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v8.3.0.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1749,7 +1754,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v8.2.10/pubnub.8.2.10.js
location: https://github.com/pubnub/javascript/releases/download/v8.3.0/pubnub.8.3.0.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v8.3.0
November 14 2024

#### Added
- Add custom message type support for the following APIs: publish, signal, share file, subscribe and history.

## v8.2.10
October 31 2024

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.10.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.2.10.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.3.0.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.8.3.0.min.js
2. Configure your keys:
Expand Down
37 changes: 24 additions & 13 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3948,7 +3948,7 @@
return base.PubNubFile;
},
get version() {
return '8.2.10';
return '8.3.0';
},
getVersion() {
return this.version;
Expand Down Expand Up @@ -6230,6 +6230,8 @@
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (decryptionError)
event.error = decryptionError;
return event;
Expand All @@ -6245,6 +6247,8 @@
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
return event;
}
messageActionFromEnvelope(envelope) {
Expand Down Expand Up @@ -6296,6 +6300,8 @@
};
}
}
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (errorMessage)
event.error = errorMessage;
return event;
Expand Down Expand Up @@ -8303,8 +8309,10 @@
return `/publish/${keySet.publishKey}/${keySet.subscribeKey}/0/${encodeString(channel)}/0${!this.parameters.sendByPost ? `/${encodeString(stringifiedPayload)}` : ''}`;
}
get queryParameters() {
const { meta, replicate, storeInHistory, ttl } = this.parameters;
const { customMessageType, meta, replicate, storeInHistory, ttl } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
if (storeInHistory !== undefined)
query.store = storeInHistory ? '1' : '0';
if (ttl !== undefined)
Expand Down Expand Up @@ -8380,6 +8388,13 @@
const stringifiedPayload = JSON.stringify(message);
return `/signal/${publishKey}/${subscribeKey}/0/${encodeString(channel)}/0/${encodeString(stringifiedPayload)}`;
}
get queryParameters() {
const { customMessageType } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
return query;
}
}

/**
Expand Down Expand Up @@ -9151,13 +9166,7 @@
if (payload.message_type === null)
payload.message_type = PubNubMessageType.Message;
const processedPayload = this.processPayload(channel, payload);
const item = {
channel,
timetoken: payload.timetoken,
message: processedPayload.payload,
messageType: payload.message_type,
uuid: payload.uuid,
};
const item = Object.assign(Object.assign({ channel, timetoken: payload.timetoken, message: processedPayload.payload, messageType: payload.message_type }, (payload.custom_message_type ? { customMessageType: payload.custom_message_type } : {})), { uuid: payload.uuid });
if (payload.actions) {
const itemWithActions = item;
itemWithActions.actions = payload.actions;
Expand All @@ -9183,8 +9192,10 @@
return `/v3/${endpoint}/sub-key/${subscribeKey}/channel/${encodeNames(channels)}`;
}
get queryParameters() {
const { start, end, count, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeMessageType ? { include_message_type: 'true' } : {}));
const { start, end, count, includeCustomMessageType, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken, } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeCustomMessageType !== undefined && includeCustomMessageType !== null
? { include_custom_message_type: includeCustomMessageType ? 'true' : 'false' }
: {})), (includeMessageType ? { include_message_type: 'true' } : {}));
}
/**
* Parse single channel data entry.
Expand Down Expand Up @@ -9461,8 +9472,8 @@
return `/v1/files/publish-file/${publishKey}/${subscribeKey}/0/${encodeString(channel)}/0/${encodeString(this.prepareMessagePayload(fileMessage))}`;
}
get queryParameters() {
const { storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
const { customMessageType, storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (customMessageType ? { custom_message_type: customMessageType } : {})), (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
}
/**
* Pre-process provided data.
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const makeConfiguration = (base, setupCryptoModule) => {
return base.PubNubFile;
},
get version() {
return '8.2.10';
return '8.3.0';
},
getVersion() {
return this.version;
Expand Down
14 changes: 5 additions & 9 deletions lib/core/endpoints/fetch_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,7 @@ class FetchMessagesRequest extends request_1.AbstractRequest {
if (payload.message_type === null)
payload.message_type = History.PubNubMessageType.Message;
const processedPayload = this.processPayload(channel, payload);
const item = {
channel,
timetoken: payload.timetoken,
message: processedPayload.payload,
messageType: payload.message_type,
uuid: payload.uuid,
};
const item = Object.assign(Object.assign({ channel, timetoken: payload.timetoken, message: processedPayload.payload, messageType: payload.message_type }, (payload.custom_message_type ? { customMessageType: payload.custom_message_type } : {})), { uuid: payload.uuid });
if (payload.actions) {
const itemWithActions = item;
itemWithActions.actions = payload.actions;
Expand All @@ -167,8 +161,10 @@ class FetchMessagesRequest extends request_1.AbstractRequest {
return `/v3/${endpoint}/sub-key/${subscribeKey}/channel/${(0, utils_1.encodeNames)(channels)}`;
}
get queryParameters() {
const { start, end, count, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeMessageType ? { include_message_type: 'true' } : {}));
const { start, end, count, includeCustomMessageType, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken, } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeCustomMessageType !== undefined && includeCustomMessageType !== null
? { include_custom_message_type: includeCustomMessageType ? 'true' : 'false' }
: {})), (includeMessageType ? { include_message_type: 'true' } : {}));
}
/**
* Parse single channel data entry.
Expand Down
4 changes: 2 additions & 2 deletions lib/core/endpoints/file_upload/publish_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class PublishFileMessageRequest extends request_1.AbstractRequest {
return `/v1/files/publish-file/${publishKey}/${subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0/${(0, utils_1.encodeString)(this.prepareMessagePayload(fileMessage))}`;
}
get queryParameters() {
const { storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
const { customMessageType, storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (customMessageType ? { custom_message_type: customMessageType } : {})), (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
}
/**
* Pre-process provided data.
Expand Down
4 changes: 3 additions & 1 deletion lib/core/endpoints/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class PublishRequest extends request_1.AbstractRequest {
return `/publish/${keySet.publishKey}/${keySet.subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0${!this.parameters.sendByPost ? `/${(0, utils_1.encodeString)(stringifiedPayload)}` : ''}`;
}
get queryParameters() {
const { meta, replicate, storeInHistory, ttl } = this.parameters;
const { customMessageType, meta, replicate, storeInHistory, ttl } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
if (storeInHistory !== undefined)
query.store = storeInHistory ? '1' : '0';
if (ttl !== undefined)
Expand Down
7 changes: 7 additions & 0 deletions lib/core/endpoints/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@ class SignalRequest extends request_1.AbstractRequest {
const stringifiedPayload = JSON.stringify(message);
return `/signal/${publishKey}/${subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0/${(0, utils_1.encodeString)(stringifiedPayload)}`;
}
get queryParameters() {
const { customMessageType } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
return query;
}
}
exports.SignalRequest = SignalRequest;
6 changes: 6 additions & 0 deletions lib/core/endpoints/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (decryptionError)
event.error = decryptionError;
return event;
Expand All @@ -231,6 +233,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
return event;
}
messageActionFromEnvelope(envelope) {
Expand Down Expand Up @@ -282,6 +286,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
}
}
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (errorMessage)
event.error = errorMessage;
return event;
Expand Down
39 changes: 39 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4764,6 +4764,10 @@ declare namespace PubNub {
userMetadata?: {
[p: string]: Payload;
};
/**
* User-provided message type.
*/
customMessageType?: string;
/**
* Sent data.
*/
Expand Down Expand Up @@ -6086,6 +6090,13 @@ declare namespace PubNub {
* The message may be any valid JSON type including objects, arrays, strings, and numbers.
*/
message: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
/**
* Whether published data should be available with `Storage API` later or not.
*
Expand Down Expand Up @@ -6150,6 +6161,13 @@ declare namespace PubNub {
* The message may be any valid JSON type including objects, arrays, strings, and numbers.
*/
message: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6566,6 +6584,10 @@ declare namespace PubNub {
* PubNub-defined message type.
*/
messageType?: PubNubMessageType.Message;
/**
* User-provided message type.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6610,6 +6632,10 @@ declare namespace PubNub {
* PubNub-defined message type.
*/
messageType?: PubNubMessageType.Files;
/**
* User-provided message type.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6652,6 +6678,12 @@ declare namespace PubNub {
* @default `100` or `25`
*/
count?: number;
/**
* Include messages' custom type flag.
*
* Message / signal and file messages may contain user-provided type.
*/
includeCustomMessageType?: boolean;
/**
* Whether message type should be returned with each history message or not.
*
Expand Down Expand Up @@ -7194,6 +7226,13 @@ declare namespace PubNub {
* File annotation message.
*/
message?: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
/**
* Custom file and message encryption key.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "8.2.10",
"version": "8.3.0",
"author": "PubNub <support@pubnub.com>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const makeConfiguration = (
return base.PubNubFile;
},
get version(): string {
return '8.2.10';
return '8.3.0';
},
getVersion(): string {
return this.version;
Expand Down
Loading

0 comments on commit 2b523b2

Please sign in to comment.