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)
Parameter | Type | Required | Description |
---|---|---|---|
data | String | Yes | The data to decrypt . |
customCipherKey | String | Optional | If 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>;
Parameter | Type | Required | Description |
---|---|---|---|
key | String | Yes | Cipher key used for decryption. |
file | PubNubFile | Yes | File 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)
Parameter | Type | Required | Description |
---|---|---|---|
data | String | Yes | The data to encrypt . |
customCipherKey | String | Optional | If it's not provided, the cipher key from config will be used. |
Basic Usage
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>;
Parameter | Type | Required | Description |
---|---|---|---|
key | String | Yes | Cipher key used for encryption. |
file | PubNubFile | Yes | File 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()
returnsPromise<Buffer>
file.toStream()
returnsPromise<Readable>
file.toString(encoding: string)
returns a string encoded usingencoding
(if not available, defaults toutf8
)
Methods supported in a browser:
file.toFile()
returnsPromise<File>
file.toBlob()
returnsPromise<Blob>
file.toArrayBuffer()
returnsPromise<ArrayBuffer>
file.toString(encoding: string)
returns returns a string encoded usingencoding
(if not available, defaults toutf8
)
React and React Native:
file.toBlob()
returnsPromise<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
// 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
pubnub.time().then((timetoken) => {
console.log(timetoken);
}).catch((error) => {
console.log(error)
});