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

static TimetokenUtils.timetokenToDate(timetoken: string | number): Date

Input

ParameterTypeRequiredDefaultDescription
timetokenstring or numberYesn/aRepresents the PubNub timetoken to convert into a Date object.

Output

TypeDescription
numberConverted Date object.

Basic usage

Convert a timetoken value of 16200000000000000 to a human-readable date and time format.

// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare a variable 'timetoken' and assign it a value of a PubNub timetoken
const timetoken = 16200000000000000

// 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
const date = TimetokenUtils.timetokenToDate(timetoken)

// 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
console.log(`Timetoken: ${timetoken}`)
console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()}`)

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

static TimetokenUtils.dateToTimetoken(date: Date): number

Input

ParameterTypeRequiredDefaultDescription
dateDateYesn/aRepresents the date and time to convert into a PubNub timetoken.

Output

TypeDescription
numberConverted 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.

Basic usage

Convert a human-readable date and time, May 3, 2021 00:00:00 GMT, to a timetoken.

// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare a "Date" object representing a specific date and time
const date = new Date('May 3, 2021 00:00:00 GMT')

// convert the "Date" object to a PubNub timetoken using the "dateToTimetoken()" method of the "TimetokenUtils" class
const timetoken = TimetokenUtils.dateToTimetoken(date)

// log the values of the date and timetoken to the console
console.log(`Date: ${date.toDateString()} ${date.toLocaleTimeString()} GMT`)
console.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:

static TimetokenUtils.unixToTimetoken(unixTime: string | number): number

Input

ParameterTypeRequiredDefaultDescription
unixTimestring or numberYesn/aRepresents the Unix timestamp to convert into a PubNub timetoken.

Output

TypeDescription
numberConverted 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.

Basic usage

Convert a Unix timestamp value of 1620000000 (representing May 3, 2021 at 12:00:00 AM GMT) to a timetoken.

// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils'

// declare the "unixTime" variable and assign it a value of 1620000000
const unixTime = 1620000000

// call the "unixToTimetoken()" method of the "TimetokenUtils" class, passing in the "unixTime" value as a parameter
// this method converts the Unix timestamp value to a PubNub timetoken and returns it
const timetoken = TimetokenUtils.unixToTimetoken(unixTime)

// log the original Unix timestamp value and the resulting timetoken value to the console
console.log(`Unix time: ${unixTime}`)
console.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

static TimetokenUtils.timetokenToUnix(timetoken: string | number): number

Input

ParameterTypeRequiredDefaultDescription
timetokenstring or numberYesn/aRepresents the PubNub timetoken to convert into a Unix timestamp.

Output

TypeDescription
numberConverted 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.

Basic usage

Convert a timetoken value of 16200000000000000 to a Unix timestamp.

// import the "TimetokenUtils" class from a module
import { TimetokenUtils } from './TimetokenUtils',

// declare a "timetoken" variable and assign it a value of a PubNub timetoken
const timetoken = 16200000000000000,

// 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
const unixTime = TimetokenUtils.timetokenToUnix(timetoken),

// 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
console.log(`Timetoken: ${timetoken}`),
console.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

static CryptoUtils.decrypt({ chat, message, decryptor, }: {
chat: Chat;
message: Message;
decryptor: (encryptedContent: string) => TextMessageContent;
}): Message

Input

ParameterTypeRequiredDefaultDescription
chatChatYesn/aInstance of the Chat SDK.
messageMessageYesn/aInstance of the unsuccessfully decrypted message, which contains the decryption error under the error field.
decryptor(encryptedContent: string) => TextMessageContentYesn/aFunction that takes an encrypted content as a string and returns the decrypted content as a TextMessageContent.

Output

TypeDescription
MessageReturned Message object with decrypted content and the error field on the Message object set to undefined (no decryption error).

Basic usage

Manually decrypt a message with the cipher key enigma.

// assuming the CryptoUtils class and its decrypt method are defined

// sample message to be decrypted
const encryptedChat = /* ... */; // provide the encrypted content here

// decrypt the message with the cipher key "enigma"
const decryptedMessage = CryptoUtils.decrypt({
chat: /* ... */, // provide the chat context
message: /* ... */, // provide the message to be decrypted
decryptor: (encryptedContent) => {
// assuming "enigma" is the cipher key for decryption
const cryptoModule = CryptoModule.aesCbcCryptoModule({
cipherKey: "enigma",
});

show all 26 lines
Last updated on