On this page

Send files

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

You can attach multiple files to a single message using Unity Chat SDK. They can be of any format, 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 takes 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 have File Sharing enabled for your app's keyset in the Admin Portal, with the selected storage region and 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 a draft message and publish it using the Send() method, or send files directly using the SendText() method.

Method signature

To add files, you must add elements to the Files property (List<ChatInputFile>) of a MessageDraft object.

Alternatively, you can pass files directly to the SendText() method using the SendTextParams.Files property.

ChatInputFile is a struct with the following properties:

PropertyDescription
Name
Type: string
Name of the file, like document.pdf.
Type
Type: string
MIME type of the file, like application/pdf.
Source
Type: string
Path to the file on the local filesystem.

Sample code

Attach a file to a text message using SendText().

1

Attach a file to a draft message using MessageDraft.Files.

1

Get all message files

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

Property signature

This property has the following signature:

1message.Files
PropertyDescription
Files
Type: List<ChatFile>
List of files attached to the message.

ChatFile contains the following properties:

PropertyDescription
Name
Type: string
Name of the file, like error-1.jpg.
Id
Type: string
Unique identifier assigned to the file by PubNub, like 736499374.
Url
Type: string
File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg.
Type
Type: string
MIME type of the file, like image/jpeg.

Sample code

List all files attached to a message.

1

Get all channel files

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

Method signature

This method takes the following parameters:

1channel.GetFiles(
2 int limit = 0,
3 string next = ""
4)

Input

* required
ParameterDescription
limit
Type: int
Default:
0
Number of files to return. When set to 0, uses the default server value.
next
Type: string
Default:
""
String token to get the next batch of files.

Output

TypeDescription
Task<ChatOperationResult<ChatFilesResult>>
Returned object containing these fields: Files, Next, and Total.

ChatFilesResult contains the following properties:

PropertyDescription
Files
Type: List<ChatFile>
List containing file details.
Next
Type: 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.
Total
Type: int
Total number of files.

ChatFile contains the following properties:

PropertyDescription
Name
Type: string
Name of the file, like error-1.jpg.
Id
Type: string
Unique identifier assigned to the file by PubNub, like 736499374.
Url
Type: string
File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg.
Type
Type: string
MIME type of the file, like image/jpeg.

Sample code

List all files published on a channel.

1

Delete files

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

Deletion consequences

Once you delete the file, you can't 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:

1channel.DeleteFile(
2 string id,
3 string name
4)

Input

* required
ParameterDescription
id *
Type: string
Default:
n/a
Unique identifier assigned to the file by PubNub.
name *
Type: string
Default:
n/a
Name of the file.

Output

TypeDescription
Task<ChatOperationResult>
Operation result indicating the success or failure of the deletion.

Sample code

Remove a file from a channel.

1

Last updated on