Send a file to a channel

Showing JavaScript examples.

This guide shows you how to upload a file to a PubNub channel. When the upload completes, PubNub delivers a file event to all subscribers on that channel.

The examples on this page are written for the current SDK releases: C# 9.0.0, Dart 8.0.3, Go v10.0.0, Java 6.4.5, JavaScript 13.0.3 (web, Node.js, and React Native tabs), Objective-C 7.0.4, PHP 9.0.3, Python 10.7.2, and Swift 10.2.0. Each example assumes a PubNub client initialized for its SDK, unless the example sets one up itself.

Prerequisite: File Sharing must be enabled on your keyset in the Admin Portal. For how the feature works, see Files.

File size limit​

FunctionLimitNotes
File size5 MBTo raise it, contact support@pubnub.com.
File retentionFile retention is 1 or 7 days on the Free plan, 30 days on Starter, and Unlimited on Pro.Set it per keyset in the Admin Portal.
Files per list callA single list-files call returns up to 100 files for a channel.

Send a file​

Choose the channel and the file. You can attach an optional text caption to the upload.

Custom message types

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

The upload code differs by environment because each one selects or reads the file differently. Choose the tab for your environment.

1import PubNub from 'pubnub';
2
3const pubnub = new PubNub({
4 publishKey: 'YOUR_PUBLISH_KEY',
5 subscribeKey: 'YOUR_SUBSCRIBE_KEY',
6 userId: 'YOUR_USER_ID',
7});
8
9const input = document.querySelector('input[type="file"]');
10
11input.addEventListener('change', async () => {
12 const file = input.files[0];
13 if (!file) {
14 console.error('No file selected.');
15 return;
show all 23 lines

A successful upload returns a timetoken. Subscribers on the channel receive a file event carrying the file ID, file name, and a download URL. For how to handle that event on the subscriber side, see Receive and download files.

Encrypt a file​

To encrypt a file before upload, set a cipher key in the SDK client configuration or pass it per upload request. Any client that downloads the file must use the same key. For how encryption works, see Files.

The Python example above shows cipher_key("secret") as an inline parameter. Check your SDK's configuration reference for the client-level cipher key option.

Was this page useful?

Last updated on