File Sharing API for JavaScript SDK

Breaking changes in v9.0.0

PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0) of the Java SDK.

For more details about what has changed, refer to Java/Kotlin SDK migration guide.

Use the Files API to upload and share files up to 5 MB on PubNub. Common use cases include social apps that share images and healthcare apps that share medical records.

When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.

Send file

Upload the file to a specified channel.

This method covers the entire process of sending a file, including preparation, uploading the file to a cloud storage service, and post-uploading messaging on a channel.

For the last messaging step, sendFile internally calls the publishFileMessage method to publish a message on the channel.

The published message contains metadata about the file, such as the file identifier and name, enabling others on the channel to find out about the file and access it.

For details on the method that publishes the file message, see Publish file message.

Don't JSON serialize

You should not JSON serialize the message and meta parameters when sending signals, messages, or files as the serialization is done automatically. Pass the full object as the message/meta payload and let PubNub take care of everything for you.

Method(s)

pubnub.sendFile()
.channel(String)
.fileName(String)
.inputStream(InputStream)
.message(Object)
.shouldStore(Boolean)
.meta(Object)
.ttl(Integer)
.customMessageType(String)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel for the file.
fileName *
Type: String
Default:
n/a
Name of the file to send.
inputStream *
Type: InputStream
Default:
n/a
Input stream with file content.
message
Type: Object
Default:
n/a
Message to send along with the file to the specified channel.
shouldStore
Type: Boolean
Default:
true
Whether to store the published file message in the channel history.
meta
Type: Object
Default:
n/a
Metadata object which can be used with the filtering ability.
ttl
Type: Integer
Default:
n/a
How long the message should be stored in the channel's storage.
customMessageType
Type: String
Default:
n/a
A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.
Deprecated parameter

The cipherKey parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Returns

The sendFile() method returns PNFileUploadResult with:

Property NameTypeDescription
timetoken
Long
A representation of the timetoken when the message was published.
status
Integer
Remote call return code.
file
PNBaseFile
Uploaded file information.

PNBaseFile contains the following properties:

Property NameTypeDescription
id
Long
Id of the uploaded file.
name
String
Name of the upload file.

List channel files

Retrieve a list of files uploaded to a channel.

Method(s)

pubnub.listFiles()
.channel(String)
.limit(Integer)
.next(String)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel to get list of files.
limit
Type: Integer
Default:
100
Number of files to return. Minimum value is 1, and maximum is 100.
next
Type: String
Default:
n/a
Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.

Sample code


Response

{
"data":[
{
"name":"cat_picture.jpg",
"id":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
"size":25778,
"created":"2025-05-27T13:55:35Z"
}],
"status": 200
"count": 1,
"next": null
}

Returns

The listFiles() method returns PNListFilesResult with:

Property NameTypeDescription
timetoken
Long
A representation of the timetoken when the message was published.
status
Integer
Remote call return code.
next
String
Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
count
Integer
Number of files returned.
data
List
List of channel files.

PNUploadedFile contains the following properties:

Property NameTypeDescription
id
Long
Id of the uploaded file.
name
String
Name of the upload file.
size
Integer
Size of the uploaded file.
created
String
Time of creation.

Get file URL

Generate a URL that can be used to download a file from the target channel.

Method(s)

pubnub.getFileUrl()
.channel(String)
.fileName(String)
.fileId(String)
* required
ParameterDescription
channel *
Type: String
Name of channel to which the file has been uploaded.
fileName *
Type: String
Name under which the uploaded file is stored.
fileId *
Type: String
Unique identifier for the file, assigned during upload.

Sample code


Response

{
"url":"http://ps.pndsn.com/v1/files/demo/channels/my_channel/files/d9515cb7-48a7-41a4-9284-f4bf331bc770/cat_picture.jpg?pnsdk=PubNub-Java-Unified/4.32.0&timestamp=1595771548&uuid=pn-9ce9e988-8e04-40bf-90c4-ebe170478f7d"
}

Returns

The getFileUrl() method returns PNFileUrlResult with:

Property NameTypeDescription
url
String
URL to be used to download the requested file.

Download file

Download a file from the specified channel.

Method(s)

pubnub.downloadFile()
.channel(String)
.fileName(String)
.fileId(String)
* required
ParameterDescription
channel *
Type: String
Name of channel to which the file has been uploaded.
fileName *
Type: String
Name under which the uploaded file is stored.
fileId *
Type: String
Unique identifier for the file, assigned during upload.
Deprecated parameter

The cipherKey parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Sample code


Response

{
"fileName": "cat_picture.jpg",
"byteStream": <file data>
}

Returns

The downloadFile() method returns PNDownloadFileResult with:

Property NameTypeDescription
fileName
String
Name of the downloaded file.
byteStream
InputStream
Input stream containing all bytes of the downloaded file.

Delete file

Delete a file from the specified channel.

Method(s)

pubnub.deleteFile()
.channel(String)
.fileName(String)
.fileId(String)
* required
ParameterDescription
channel *
Type: String
The channel from which to delete the file.
fileName *
Type: String
Name of the file to be deleted.
fileId *
Type: String
Unique identifier of the file to be deleted.

Sample code


Response

{
"status": 200
}

Returns

The deleteFile() method returns PNDeleteFileResult with:

Property NameTypeDescription
Status
Integer
Returns a status code.

Publish file message

Publish the uploaded file message to a specified channel.

This method is called internally by sendFile as part of the file-sending process to publish the message with the file (already uploaded in a storage service) on a channel.

This message includes the file's unique identifier and name elements, which are needed to construct download links and inform channel subscribers that the file is available for download.

You can call this method when sendFile fails and returns the status.operation === PNPublishFileMessageOperation error. In that case, you can use the data from the status object to try again and use publishFileMessage to manually resend a file message to a channel without repeating the upload step.

Don't JSON serialize

You should not JSON serialize the message and meta parameters when sending signals, messages, or files as the serialization is done automatically. Pass the full object as the message/meta payload and let PubNub take care of everything for you.

Method(s)

pubnub.publishFileMessage()
.channel(String)
.fileName(String)
.fileId(String)
.message(Object)
.meta(Object)
.shouldStore(Boolean)
.customMessageType(String)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Name of channel to publish file message.
fileName *
Type: String
Default:
n/a
Name of the file.
fileId *
Type: String
Default:
n/a
Unique identifier of the file.
message
Type: Object
Default:
n/a
The payload.
meta
Type: Object
Default:
n/a
Metadata object which can be used with the filtering ability.
shouldStore
Type: Boolean
Default:
true
Whether to store this message in history. Set to false to not store it. By default, messages are stored according to the retention policy you set on your key.
customMessageType
Type: String
Default:
n/a
A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.

Sample code


Response

[1, "Sent", "17483548017978763"]

Returns

The publishFileMessage() method returns PNFileUploadResult with:

Property NameTypeDescription
timetoken
Long
The timetoken at which the message was published.
status
Integer
Remote call return code.
file
PNBaseFile
Uploaded file information.

PNBaseFile contains the following properties:

Property NameTypeDescription
id
Long
Unique identifier of the uploaded file
name
String
Name of the uploaded file
Last updated on