I am trying to move all sendbird sdk logic in a web app from the main thread to a shared worker context, and it’s been working great except for the channel.sendFileMessage function. I am getting an unexpected
{message: "Invalid value: "JSON body.".", code: 400403, error: true}
error every time I try to upload an image in a message.
we are using the javascript v3 sdk by the way:
this is the function running in the worker context:
export async function sendImageMessage(
tabId: string,
file: File,
data: unknown = {},
): Promise<SendbirdClient.FileMessage> {
const tabState = getTabIdState(tabId);
if (!tabState) throw new Error('tabState is not initialized');
return new Promise<SendbirdClient.FileMessage>((resolve, reject) => {
if (!tabState.channel) {
reject(new Error('Channel is undefined'));
return;
}
const client = getSendbirdClientInstance();
const params = new client.FileMessageParams();
params.file = file;
params.data = JSON.stringify(data);
tabState.channel.sendFileMessage(params, (message, error) => {
if (error) {
console.log('[WW] failed to send image message on channel', tabState.channel?.url, error);
reject(error);
} else {
console.log('[WW] finished sending image file on channel', tabState.channel?.url);
resolve(message as SendbirdClient.FileMessage);
}
});
});
}
current working code(running in the main thread):
async sendImageMessage(
file: File,
data: unknown = {},
): Promise<SendBirdClient.FileMessage> {
return new Promise<SendBirdClient.FileMessage>((resolve, reject) => {
if (this.channel) {
const params = new this.client.FileMessageParams();
params.file = file;
params.data = JSON.stringify(data);
this.channel.sendFileMessage(params, (message, error) => {
error
? reject(error)
: resolve(message as SendBirdClient.FileMessage);
});
} else {
reject(new Error('Channel is undefined'));
}
});
}
I see some differences in the form data sent to the /v3/storage/file api:
As you can see, there is no difference in how the sdk functions are called, however I wonder if the sdk has any limitation when it is run in a worker context and tries to read a file, it looks like the file’s binary data is not included when the api is called from the worker context. Any help on this matter will be highly appreciated.