PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

javascript

  • Getting Started
  • API Reference

    • Configuration
    • Publish & Subscribe
    • Presence
    • Access Manager
    • Channel Groups
    • Message Persistence
    • Mobile Push
    • Objects
    • Files
    • Message Actions
    • Miscellaneous
  • Status Events
  • Troubleshooting
  • Change Log
  • Feature Support
  • Platform Support

Utility Methods API for PubNub JavaScript SDK

The methods on this page are utility methods that don't fit into other categories.

Close PubNub

Description

End all open requests and close the PubNub instance.

Basic Usage

pubnub.stop();

Decrypt

Description

This function allows to decrypt the data.

Method(s)

To decrypt the data, you can use the following method(s) in JavaScript V4 SDK.

decrypt(data: string, customCipherKey: ?string)
ParameterTypeRequiredDescription
dataStringYesThe data to decrypt.
customCipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.

Basic Usage

var decrypted = pubnub.decrypt(encrypted, 'myCipherKey'); // Pass the encrypted data as the first parameter in decrypt Method

Returns

It returns the decrypted data as an object.

Decrypt File

Description

This function allow to decrypt the file content/data.

Method(s)

To decrypt the file, you can use the following method(s) in JavaScript V4 SDK.

pubnub.decryptFile(key: string, file: PubNubFile): Promise<PubNubFile>;
ParameterTypeRequiredDescription
keyStringYesCipher key used for decryption.
filePubNubFileYesFile to decrypt.

Basic Usage

// Node.js example
import fs from 'fs';

const fileBuffer = fs.readFileSync('./cat_picture_encrypted.jpg');

const file = pubnub.File.create({ data: fileBuffer, name: 'cat_picture.jpg', mimeType: 'image/jpeg' });

const decryptedFile = await pubnub.decryptFile('myCipherKey', file);

Returns

Returns a promise of PubNubFile

Encrypt

Description

This function allows to encrypt the data.

Method(s)

To encrypt the data, you can use the following method(s) in JavaScript V4 SDK.

encrypt(data: string, customCipherKey: ?string)
ParameterTypeRequiredDescription
dataStringYesThe data to encrypt.
customCipherKeyStringOptionalIf it's not provided, the cipher key from config will be used.

Basic Usage

Encrypt part of message

var sCypher = "testCypher";
var msgContent = "This is the data I wish to encrypt.";
console.log('msgContent: ' + msgContent);

// Encrypt with pubnub
var encryptedMessage = pubnub.encrypt(JSON.stringify(msgContent), sCypher);
console.log('encryptedMessage PN: ' + encryptedMessage);

Returns

It returns the encrypted data as string.

Encrypt File

Description

This function allow to encrypt the file content/data.

Method(s)

To encrypt the file, you can use the following method(s) in JavaScript V4 SDK.

pubnub.encryptFile(key: string, file: PubNubFile): Promise<PubNubFile>;
ParameterTypeRequiredDescription
keyStringYesCipher key used for encryption.
filePubNubFileYesFile to encrypt.

Basic Usage

// Node.js example
import fs from 'fs';

const fileBuffer = fs.readFileSync('./cat_picture.jpg');

const file = pubnub.File.create({ data: fileBuffer, name: 'cat_picture.jpg', mimeType: 'image/jpeg' });

const encryptedFile = await pubnub.encryptFile('myCipherKey', file);

Returns

Returns a promise of PubNubFile

PubNubFile

Description

Internal representation of the file used by the SDK. Depending on the environment, different methods can be used to extract the file.

Extracting the file

Methods supported in Node.js:
  • file.toBuffer() returns Promise<Buffer>
  • file.toStream() returns Promise<Readable>
  • file.toString(encoding: string) returns a string encoded using encoding (if not available, defaults to utf8)
Methods supported in a browser:
  • file.toFile() returns Promise<File>
  • file.toBlob() returns Promise<Blob>
  • file.toArrayBuffer() returns Promise<ArrayBuffer>
  • file.toString(encoding: string) returns returns a string encoded using encoding (if not available, defaults to utf8)
React and React Native:
  • file.toBlob() returns Promise<Blob>

Creating a file

pubnub.File.create(input: FileInput): PubNubFile;

FileInput represents a variety of possible inputs that represent a file in different environments.

Node.js

  • Using streams:

    {
        stream: Readable,
        name: string,
        mimeType?: string
    }
    
  • Using buffers:

    {
        data: Buffer,
        name: string,
        mimeType?: string
    }
    
  • Using strings:

    {
        data: string,
        encoding: string,
        name: string,
        mimeType?: string
    }
    

Browsers

  • Using File API

    File
    
  • Using strings:

    {
        data: string,
        name: string,
        mimeType?: string
    }
    
  • Using ArrayBuffer:

    {
        data: ArrayBuffer,
        name: string,
        mimeType?: string
    }
    

Reconnect

Description

Call the reconnect method to force the SDK to try and reach out PubNub.

Method(s)

To reconnect the data, you can use the following method(s) in JavaScript V4 SDK.

reconnect()

This method doesn't take any arguments.

Basic Usage

pubnub.reconnect();

Time

Description

This function will return a 17 digit precision Unix epoch.

The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000

Example of creating a timetoken for a specific time and date

08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000

Method(s)

To fetch Time, you can use the following method(s) in JavaScript V4 SDK

const timetoken = await pubnub.time();

Basic Usage

Get PubNub Timetoken

// assuming an initialized PubNub instance already exists
try {
    const timetoken = await pubnub.time();
} catch (status) {
    console.log("Something went wrong:", status);
}

Response

//Example of status
{
    error: false,
    operation: 'PNTimeOperation',
    statusCode: 200
}

//Example of response
{
    timetoken: 15031768233407550
}

Other Examples

Basic usage with Promises

pubnub.time().then((timetoken) => {
    console.log(timetoken);
}).catch((error) => {
    console.log(error)
});
← Message ActionsStatus Events →
  • Close PubNub
    • Description
    • Basic Usage
  • Decrypt
    • Description
    • Method(s)
    • Basic Usage
    • Returns
  • Decrypt File
    • Description
    • Method(s)
    • Basic Usage
    • Returns
  • Encrypt
    • Description
    • Method(s)
    • Basic Usage
    • Returns
  • Encrypt File
    • Description
    • Method(s)
    • Basic Usage
    • Returns
  • PubNubFile
    • Description
    • Extracting the file
    • Creating a file
  • Reconnect
    • Description
    • Method(s)
    • Basic Usage
  • Time
    • Description
    • Method(s)
    • Basic Usage
    • Response
    • Other Examples
© PubNub Inc. - Privacy Policy