Utility methods
This section lists various helper methods that provide a convenient way to work with PubNub timetokens.
Timetoken to date
The timetokenToDate() method of the TimetokenUtils class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Date object representing the corresponding date and time.
Use this method when you want to display the timetoken of each message or event in the chat history in a human-readable format.
Method signature
1static TimetokenUtils.timetokenToDate(timetoken: string | number): Date
Input
| Parameter | Description |
|---|---|
timetoken *Type: string or numberDefault: n/a | Represents the PubNub timetoken to convert into a Date object. |
Output
| Type | Description |
|---|---|
number | Converted Date object. |
Sample code
Convert a timetoken value of 16200000000000000 to a human-readable date and time format.
1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare a variable 'timetoken' and assign it a value of a PubNub timetoken
5const timetoken = 16200000000000000
6
7// declare a "date" variable and assign it a "Date" object representing the corresponding date and time by calling the "timetokenToDate()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
8const date = TimetokenUtils.timetokenToDate(timetoken)
9
10 // log the values of the timetoken and date to the console using the "toDateString" and "toLocaleTimeString" methods to format the date in a human-readable date and time format
11console.log(`Timetoken: ${timetoken}`)
12console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()}`)
13
The output of the method is as follows:
Timetoken: 16200000000000000
Date: Mon May 03 2021 8:26:40 PM
Date to timetoken
The dateToTimetoken() method of the TimetokenUtils class converts the Date object representing the corresponding date and time into a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).
Use this method to represent a specific date and time as a PubNub timetoken value, for example, to retrieve messages from a PubNub channel at a particular date and time.
Method signature
1static TimetokenUtils.dateToTimetoken(date: Date): number
Input
| Parameter | Description |
|---|---|
date *Type: DateDefault: n/a | Represents the date and time to convert into a PubNub timetoken. |
Output
| Type | Description |
|---|---|
number | Converted timetoken value. |
Errors
If the passed value is not a valid Date object, you'll get the exception The value passed as date is not an instance of Date. If the passed Date object is valid, the method will return a number value representing the corresponding PubNub timetoken for that date and time.
Sample code
Convert a human-readable date and time, May 3, 2021 00:00:00 GMT, to a timetoken.
1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare a "Date" object representing a specific date and time
5const date = new Date('May 3, 2021 00:00:00 GMT')
6
7// convert the "Date" object to a PubNub timetoken using the "dateToTimetoken()" method of the "TimetokenUtils" class
8const timetoken = TimetokenUtils.dateToTimetoken(date)
9
10// log the values of the date and timetoken to the console
11console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()} GMT`)
12console.log(`Timetoken: ${timetoken}`)
Date: Mon May 03 2021 12:00:00 GMT+0000 (GMT)
Timetoken: 16200000000000000
Unix timestamp to timetoken
The unixToTimetoken() method of the TimetokenUtils class converts a Unix timestamp (a number of seconds since January 1, 1970) to a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970).
Use this method when you need a timetoken to retrieve historical messages with a specific timestamp range from Message Persistence.
Method signature
This method takes the following parameters:
1static TimetokenUtils.unixToTimetoken(unixTime: string | number): number
Input
| Parameter | Description |
|---|---|
unixTime *Type: string or numberDefault: n/a | Represents the Unix timestamp to convert into a PubNub timetoken. |
Output
| Type | Description |
|---|---|
number | Converted timetoken value. |
Errors
If the passed value is not a number (NaN()), you'll get the exception The value passed as unixTime is NaN. If the number is valid, the unixToTimetoken() method multiplies it by 10000 and returns the timetoken value.
Sample code
Convert a Unix timestamp value of 1620000000 (representing May 3, 2021 at 12:00:00 AM GMT) to a timetoken.
1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils'
3
4// declare the "unixTime" variable and assign it a value of 1620000000
5const unixTime = 1620000000
6
7// call the "unixToTimetoken()" method of the "TimetokenUtils" class, passing in the "unixTime" value as a parameter
8// this method converts the Unix timestamp value to a PubNub timetoken and returns it
9const timetoken = TimetokenUtils.unixToTimetoken(unixTime)
10
11// log the original Unix timestamp value and the resulting timetoken value to the console
12console.log(`Unix time: ${unixTime}`)
13console.log(`Timetoken: ${timetoken}`)
The output of the method is as follows:
Unix time: 1620000000
Timetoken: 16200000000000000
Timetoken to Unix timestamp
The timetokenToUnix() method of the TimetokenUtils class converts a PubNub timetoken (a unique identifier for each message sent and received in a PubNub channel that is a number of 100-nanosecond intervals since January 1, 1970) to a Unix timestamp (a number of seconds since January 1, 1970).
Use this method to convert PubNub timetoken for use in another context or system that requires a Unix timestamp.
Method signature
1static TimetokenUtils.timetokenToUnix(timetoken: string | number): number
Input
| Parameter | Description |
|---|---|
timetoken *Type: string or numberDefault: n/a | Represents the PubNub timetoken to convert into a Unix timestamp. |
Output
| Type | Description |
|---|---|
number | Converted Unix timestamp value. |
Errors
If the passed value is not a number (NaN()), you'll get the exception The value passed as timetoken is NaN. If the number is valid, the timetokenToUnix() method divides the timetoken value by 10000 to obtain the Unix timestamp value.
Sample code
Convert a timetoken value of 16200000000000000 to a Unix timestamp.
1// import the "TimetokenUtils" class from a module
2import { TimetokenUtils } from './TimetokenUtils',
3
4// declare a "timetoken" variable and assign it a value of a PubNub timetoken
5const timetoken = 16200000000000000,
6
7// declare a "unixTime" variable and assign it a Unix timestamp value by calling the "timetokenToUnix()" method of the "TimetokenUtils" class, passing the timetoken as a parameter
8const unixTime = TimetokenUtils.timetokenToUnix(timetoken),
9
10// log the value of "timetoken" and the resulting "unixTime" to the console using the date/time format that is commonly used in computer systems and programming languages
11console.log(`Timetoken: ${timetoken}`),
12console.log(`Unix time: ${unixTime}`),
The output of the method is as follows:
Timetoken: 16200000000000000
Unix time: 1620000000
Decrypt messages/files
The decrypt() method of the CryptoUtils class lets you manually decrypt a previously encrypted message (and files) when it fails to be automatically decrypted by the crypto module.
For example, you can use this method to decrypt a message encrypted with a cipher key that is different than the one you use now for encrypting messages. However, you must know the original cipher key used for encrypting the message to decrypt it manually.
Method signature
1static CryptoUtils.decrypt({ chat, message, decryptor, }: {
2 chat: Chat;
3 message: Message;
4 decryptor: (encryptedContent: string) => TextMessageContent;
5}): Message
Input
| Parameter | Description |
|---|---|
chat *Type: ChatDefault: n/a | Instance of the Chat SDK. |
message *Type: MessageDefault: n/a | Instance of the unsuccessfully decrypted message, which contains the decryption error under the error field. |
decryptor *Type: (encryptedContent: string) => TextMessageContentDefault: n/a | Function that takes an encrypted content as a string and returns the decrypted content as a TextMessageContent. |
Output
| Type | Description |
|---|---|
Message | Returned Message object with decrypted content and the error field on the Message object set to undefined (no decryption error). |
Sample code
Manually decrypt a message with the cipher key enigma.
1// assuming the CryptoUtils class and its decrypt method are defined
2
3// sample message to be decrypted
4const encryptedChat = /* ... */; // provide the encrypted content here
5
6// decrypt the message with the cipher key "enigma"
7const decryptedMessage = CryptoUtils.decrypt({
8 chat: /* ... */, // provide the chat context
9 message: /* ... */, // provide the message to be decrypted
10 decryptor: (encryptedContent) => {
11 // assuming "enigma" is the cipher key for decryption
12 const cryptoModule = CryptoModule.aesCbcCryptoModule({
13 cipherKey: "enigma",
14 });
15
show all 26 lines