Is there a way to make sure that the `socket.on('upload-progress')` listener accurately updates the upload progress for each video as multiple videos are uploaded simultaneously? Currently, it appears that the listener is updating the progress for all videos indiscriminately, regardless of their respective indexes. The provided `handleUploadLectureVideo` function manages the video uploads and progress updates:
async function handleUploadLectureVideo({ files }: any, index: number) {
const file = files[0];
const formData = new FormData();
formData.append("file", file);
formData.append("name", file.name);
socket.emit("upload-file", {
file,
fileMeta: {
originalName: file.name,
},
name: file.name,
});
console.log("index outside", index);
// Setting video upload progress
socket.on("upload-progress", function (data: number) {
console.log("index", index);
setValue(`courseLectures.${index}.uploadingProgress`, data);
});
// Setting video ID into payload after successful video upload
socket.on("upload-success", function (data: any) {
setValue(`courseLectures.${index}.lectureVideo`, data.id);
});
}
The backend code (Nest JS) for uploading videos is as follows:
async handleFileUpload(@MessageBody() data: any, @ConnectedSocket() client: Socket) {
const file = data.file;
const meta = data.fileMeta;
const name = data.name;
const filename = this.getCompleteFileName(meta);
const completePath = join('video-lectures', filename);
await promises.writeFile(completePath, file);
this.vimeoClient.upload(
completePath,
{ name },
async (uri: string) => {
const video = new Video();
video.url = uri;
const result = await this.videoRepository.save(video);
client.emit('upload-success', result);
},
(bytesUploaded: number, bytesTotal: number) => {
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
client.emit('upload-progress', percentage);
},
error => {
console.log('Failed because: ' + error);
client.emit('upload-failed', error);
},
);
}
It has been observed that the index value within the `socket.on('upload-progress')` and `socket.on('upload-success')` listeners does not align correctly with the video being uploaded, resulting in inaccurate progress and video IDs being assigned to the wrong videos. How can this code be adjusted to ensure that the correct video progress and IDs are associated with the corresponding videos during simultaneous uploads?
An attempt was made to simultaneously upload multiple videos using the `handleUploadLectureVideo` function provided, but discrepancies were noticed in progress updates and video ID assignments. Specifically, the `index` value within the `socket.on('upload-progress')` and `socket.on('upload-success')` listeners did not match the expected video index, leading to progress and IDs being allocated incorrectly.
It was anticipated that the `index` value passed to the `handleUploadLectureVideo` function would be accurately captured within the `socket.on` listeners, ensuring that progress and ID updates for each video would be linked to the correct index.