File Sharing

PubNub makes it easy to add file sharing alongside messages and signals.

Use file sharing to share images in social apps or send medical records in healthcare apps. You can easily upload and share videos, images, or documents up to 5 MB per file.

Turn on File Sharing to start sending files with your keyset.

Configuration

Enable and configure File Sharing for your app's keyset in the Admin Portal.

Public Admin Portal demo

Want to browse through the Admin Portal without creating an account? Explore it through the Public Demo that shows examples of most PubNub features for transport and logistics use case.

The Admin Portal prompts you to enable File Sharing when you create a keyset (unless you choose Choose later for Region).

Testing keysets default to the US East region with 7‑day retention. Retention is how long PubNub keeps files before deletion.

File Sharing settings in Admin Portal showing bucket region and retention controls

OptionDescription
Bucket Region
The geographical region where PubNub stores and manages your files.

Align file retention with your Message Persistence settings to keep messages and files for the same duration.
Retention
The duration that PubNub keeps files. Free accounts support 1 or 7 days. Paid accounts support higher limits.

Send files

To send a file, choose the channel and the file. You can add a text caption.

You can encrypt files using Advanced Encryption Standard (AES-256) (see Data Security). Set a cipher key in the client configuration or pass it per request. Clients that download the file must use the same key.

The following code sends the file cat_picture.jpg of the custom type file-message on the channel my_channel.

Custom message types

Not all SDKs support sending messages with a custom message type. For details, see Message Types.

// web
const input = document.querySelector('input[file]');

input.addEventListener('change', async () => {
const file = input.files[0];

const result = await pubnub.sendFile({
channel: 'my_channel',
file: file,
customMessageType: 'file-message'
});
});

// Node.js
import fs from 'fs';
show all 36 lines

The client uploads files, and the storage service stores and manages the files for each keyset.

After upload, channel subscribers receive a file message with a description, file name, and file ID. Use an SDK method to build a URL to view or download the file. For details, see Retrieve files.

Enable the feature

File Sharing is an optional feature that you must enable for your keys in the Admin Portal.

File operations

You can list files in a channel or delete uploaded files. If the keyset has Message Persistence, you can search historical file messages with the Message Persistence API. Use the results to display or download files.

Refer to the File Sharing section in the SDK documentation for details about file operations in each SDK.

Receive files

To receive a file, subscribe to the channel and listen for file events. No special subscription is required.

Retrieve files

Retrieving files takes two steps. Use the Message Persistence API and File Sharing SDK methods. To retrieve a file:

  1. Get the file message by using the Message Persistence API or the listFiles method.
  2. Display or download the file by using dedicated SDK methods and the returned file information.
File URLs

The Message Persistence API and the listFiles method return secure file metadata (such as file name and ID). Use SDK methods to generate file URLs for viewing or downloading.

When you fetch file messages with the Message Persistence API, the response includes the file ID, file name, and message type 4 (file message). Use this data to view or download the file.

Display and download files

Apart from the optional description, a file message contains the file ID and file name.

A file message has no URL. Use an SDK method to build the file URL to view or download the file.

The following code returns the URL of the cat_picture.jpg file sent to the channel my_channel:

const result = pubnub.getFileUrl({ channel: 'my_channel', id: 'd9515cb7-48a7-41a4-9284-f4bf331bc770', name: 'cat_picture.jpg' });

The call returns a file URL. Use it to view the file in the browser.

Use the same file information to download the file. The following example downloads cat_picture.jpg from my_channel:

// web
const file = await pubnub.downloadFile({
channel: 'my_channel',
id: '...',
name: 'cat_picture.jpg',
});

const myImageTag = document.createElement('img');
myImageTag.src = URL.createObjectURL(await file.toFile());

document.body.appendChild(myImageTag);

// Node.js using streams
import fs from 'fs'

show all 38 lines

List files

To list files (without file messages), request up to 100 files for a channel. If more files exist, the response includes more for pagination.

The following example returns the name, ID, size, and created timestamp for up to 100 files sent to the channel my_channel:

const result = await pubnub.listFiles({ channel: 'my_channel' });
Last updated on