Send files

Attach files to text messages to share information (support chats) or improve collaboration (social conversations).

Contrary to the Swift SDK, Swift Chat SDK lets users attach not one but multiple files to a single message. They can attach any format of the files, but the size of each file must be at most 5 MB.

Files are uploaded to a message sequentially - the more files you attach, the longer it will take for the files to upload. To improve user experience in your chat app, you can implement a progress bar indicating to the user how many files have already been uploaded.

Requires File Sharing

To store files with PubNub, you must enable File Sharing for your app's keyset in the Admin Portal, choose the storage region, and set the desired file retention period. Consider aligning the file retention period with the retention you set for Message Persistence to store messages and files for the same period of time.

Send files

You can attach files to the message and publish it using the sendText() method.

Method signature

Head over to the sendText() method section for details.

Basic usage

Attach two files of different formats to a text message.

// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Join the channel
channel.join(completion: { joinResult in
switch joinResult {
case .success:
// Handle success on joining
// Define the files to be attached
let files = [
InputFile(
name: "document.pdf",
show all 51 lines

Get all message files

You can access the files property of the Message object to list all files attached to a single message.

Method signature

This method has the following signature:

message.files

Basic usage

List all files attached to a message on the support channel.

// Retrieve the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the message using the timetoken
channel.getMessage(timetoken: 16200000000000000) { messageResult in
switch messageResult {
case let .success(message):
if let message = message {
// Access the files property
let files = message.files
if !files.isEmpty {
debugPrint("The message contains the following files:")
show all 35 lines

Get all channel files

getFiles() returns all files attached to messages on a given channel.

Method signature

This method takes the following parameters:

channel.getFiles(
limit: Int = 100,
next: String? = nil,
completion: ((Swift.Result<(files: [GetFileItem], page: PubNubHashedPage?), Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
limitIntNo100Number of files to return.
nextStringNon/aString token to get the next batch of files.

Output

ParameterTypeDescription
((Swift.Result<(files: [GetFileItem], page: PubNubHashedPage?), Error>) -> Void)objectReturned object containing these fields: files and page.
 → files[GetFileItem]Array containing file details.
 → pagePubNubHashedPagePagination details.
 → → nextStringRandom 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.
 → → totalIntTotal number of files.

GetFileItem contains the following properties:

ParameterTypeDescription
nameStringName of the file, like error-1.jpg.
idStringUnique identifier assigned to the file by PubNub, like 736499374.
urlStringFile's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg.

Basic usage

List all files published on the support channel.

// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the files attached to messages on the channel
fetchFiles(channel: channel, next: nil)
}
case let .failure(error):
// Handle failure
debugPrint("Failed to get channel details: \(error.localizedDescription)")
}
}

show all 32 lines

Delete files

Delete sent files or files from published messages with the deleteFile() method.

Endpoint limitation

Due to a lack of search functionality in the PubNub Message Persistence API, once you delete the file, you cannot track which historical messages contained the deleted file to remove info about the file from those messages.

Method signature

This method takes the following parameters:

channel.deleteFile(
id: String,
name: String,
completion: ((Swift.Result<Void, Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
idStringYesn/aUnique identifier assigned to the file by PubNub.
nameStringYesn/aName of the file.

Output

TypeDescription
((Swift.Result<Void, Error>) -> Void)Status code of the response.

Basic usage

Remove a file named error-screenshot.png from the support channel.

// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")

// Delete the file named "error-screenshot.png" from the "support" channel
let fileId = "file-id-to-delete" // Replace this with the actual file ID
let fileName = "error-screenshot.png"

channel.deleteFile(id: fileId, name: fileName) { deleteResult in
switch deleteResult {
case .success:
debugPrint("File '\(fileName)' successfully deleted from 'support' channel.")
show all 26 lines
Last updated on