Utility Methods API for JavaScript SDK
The Utility Methods API of the PubNub SDK for JavaScript covers operations that fit no other API page, such as connection control, subscription queries, and the PubNub service time.
The JavaScript SDK adds two sections that other SDKs do not have: PubNubFile and setProxy.
Disconnect
pubnub.disconnect() stops the subscribe loop of a PubNub client for JavaScript but keeps its subscriptions, so a reconnect can resume them.
With the standardized subscribe and presence workflows (enableEventEngine: true) and a heartbeatInterval, disconnect() also sends a leave request for the subscribed channels and channel groups, unless isOffline is true. Without the standardized workflows, disconnect() also stops the heartbeat timer and sends no leave request.
isOffline#- Whether a network problem caused the disconnect. If
true, the client sends no leave request. Withtrue, the client also emits an error status, such asPNDisconnectedUnexpectedlyCategory, while it receives messages.isOfflinehas an effect only with the standardized subscribe and presence workflows. Without them, the client ignores it.
Sample code
Returns
disconnect() returns nothing. Monitor the result with the status listener of the client.Examples for Disconnect
Syntax
1pubnub.disconnect(isOffline?: boolean): void;
Basic usage
1
Reconnect
pubnub.reconnect() restarts the subscribe loop of a PubNub client for JavaScript after a disconnect or a failed connection. The client resumes the subscriptions that were active at the time of the disconnect.
parameters#- Catch-up cursor for the reconnect.
parametershas an effect only with the standardized subscribe and presence workflows. Without them, the client ignores it. - property of parameters:
timetoken# - The timetoken from which to start returning available cached messages. PubNub provides cached messages on a best-effort basis. If you omit
timetoken, the client continues from the cursor of its last subscribe response. - property of parameters:
region# - The region value that belongs to the cursor in
timetoken. If you omitregion, the client keeps the region of its last subscribe response.
Sample code
Returns
reconnect() returns nothing. Monitor the result with the status listener of the client.Examples for Reconnect
Syntax
1pubnub.reconnect(parameters?: {
2 timetoken?: string,
3 region?: number
4}): void;
Basic usage
1
Get subscribed channels
pubnub.getSubscribedChannels() returns the channels that a PubNub client for JavaScript subscribes to.
<channel>-pnpres) of every channel that you subscribed to with presence events. This applies only with the standardized subscribe and presence workflows.getSubscribedChannels() takes no parameters.Returns
getSubscribedChannels() returns an array of channel names, for example ["channel1", "channel2"]. The array is empty when the client has no subscriptions.Examples for Get subscribed channels
Syntax
1pubnub.getSubscribedChannels(): string[];
Get subscribed channel groups
pubnub.getSubscribedChannelGroups() returns the channel groups that a PubNub client for JavaScript subscribes to.
<channel group>-pnpres) of every channel group that you subscribed to with presence events. This applies only with the standardized subscribe and presence workflows.getSubscribedChannelGroups() takes no parameters.Returns
getSubscribedChannelGroups() returns an array of channel group names, for example ["group1", "group2"]. The array is empty when the client has no subscriptions.Examples for Get subscribed channel groups
Syntax
1pubnub.getSubscribedChannelGroups(): string[];
Time
pubnub.time() returns the current PubNub service time as a timetoken, using a PubNub client for JavaScript.
callback#- Function that the SDK calls when the request completes. With a callback,
time()returns nothing. Without it,time()returns a promise.
Returns
time() resolves with a TimeResponse. With a callback, status.operation is PNTimeOperation and the response is the second callback argument.
Examples for Time
Syntax
1pubnub.time(): Promise<TimeResponse>;
2
3pubnub.time(callback: ResultCallback<TimeResponse>): void;
Returns
1type TimeResponse = {
2 timetoken: string
3}
PubNubFile
PubNubFile is the file object that a PubNub client for JavaScript uses for file content. pubnub.File.create() builds one, and downloadFile() returns one. For the file methods, see Files.
Extracting the file
The methods that read a PubNubFile object depend on the environment. A method that the environment does not support rejects with an error.
Methods supported in Node.js
In Node.js, file.toBuffer() resolves with a Buffer, file.toStream() resolves with a Readable stream, and file.toArrayBuffer() resolves with an ArrayBuffer. The method file.toString(encoding?: string) resolves with a string encoded with encoding, which defaults to utf8. The methods toFile(), toBlob(), and toFileUri() reject.
Methods supported in a browser
In a browser, file.toFile() resolves with a File, file.toBlob() resolves with a Blob, and file.toArrayBuffer() resolves with an ArrayBuffer. The method file.toString() resolves with a string that holds the binary content of the file and takes no encoding argument. The methods toBuffer(), toStream(), and toFileUri() reject.
React and React Native
In React Native, file.toArrayBuffer() resolves with an ArrayBuffer, and file.toString() resolves with a string and takes no encoding argument. For a file that you created from a File, file.toBlob() and file.toFile() resolve with that File. For a file that you created from a string or a Blob, they resolve with a Blob. Both reject for a file that you created from an ArrayBuffer, a typed array, or a uri, and for a file that downloadFile() returns, so use toArrayBuffer() there.
In React Native, file.toFileUri() resolves with the { uri, name, type } object of a file that you created from a uri and rejects for any other file. The methods toBuffer() and toStream() reject.
Creating a file
filerequired#- Content and metadata of the file. The accepted shapes depend on the environment, as described in the next sections. Without usable content,
create()throwsCouldn't construct a file out of supplied options.Without a file name, it throwsCouldn't guess filename out of the options. Please provide one.
Node.js
In Node.js, mimeType defaults to application/octet-stream. The data property accepts a Buffer, an ArrayBuffer, or a string. For a string, the optional encoding property sets the encoding and defaults to utf8. For the object shapes, see FileInput.
Browsers
In a browser, file accepts a File or an object whose data property is a string, an ArrayBuffer, an ArrayBufferView, or a Blob. For the object shapes, see FileInput.
React Native
React Native accepts a File, an object with a data property like the one for browsers, and an object with a uri property. For the object shapes, see FileInput.
Examples for PubNubFile
Creating a file
1pubnub.File.create(file: FileInput): PubNubFile;
setProxy
pubnub.setProxy() sets or removes the HTTP or HTTPS proxy of a PubNub client for JavaScript in Node.js at run time. This method is available only in Node.js.
The client sends all its requests through the proxy until you remove it. Only explicit HTTP and HTTPS proxies work. The SDK does not support SOCKS proxies, PAC files, or proxy environment variables. After the proxy changes, the client calls reconnect() so that the subscribe loop uses the new setting.
configuration#- Proxy to use. Pass a proxy URI such as
http://proxy.example.com:8080, or an object with the fields below. CallsetProxy()without an argument to remove the proxy. - property of configuration:
hostnamerequired# - Host name or IP address of the proxy. The
hostfield is an alias, so set one of the two. - property of configuration:
host# - Alias of
hostname. - property of configuration:
port# - Port on which the proxy listens. If you omit
port, the SDK builds the proxy address without one. - property of configuration:
protocol# - Protocol of the proxy. Supported values are
httpandhttps. - property of configuration:
auth# - Basic authentication credentials of the proxy in the
user:passwordform.
Passing a proxy together with keepAlive: true in the client configuration throws a PubNubError with the message Can't set 'proxy' because already configured for 'keepAlive'. Removing the proxy never throws.
Sample code
Other examples
Examples for setProxy
Syntax
1pubnub.setProxy(configuration?: string | {
2 hostname?: string,
3 host?: string,
4 port?: number,
5 protocol?: string,
6 auth?: string
7}): void;
Basic usage
1