peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

feat: Notification based BufferedDataConnection

Open ProPablo opened this issue 2 years ago • 0 comments

I have the use case of Datachannel of trying to send files from one client to the other. The speed is not particularly an issue anymore but I would still like to have Notifications/events for when a chunk is complete to create Progress Bars.

This PR addresses that. If connecting with the serialization 'notify', the Dataconnection will emit an additional event for when a chunk is done sending. The user can subscribe to this and use the information as they please. An example of this is:

var arr = await inputFile.arrayBuffer();
const blob = new Blob([inputFile]);
const nextId = conn.nextID;

conn.on('sentChunk', (chunk) => {
	if (chunk.id === nextId) {
		console.log('Sent chunk', chunk);
		if (chunk.n == chunk.total - 1) {
			console.log('Sent last chunk');
		}
	}
});
conn.send(arr);

I also have some ideas on what to do for the other Stream based serializations so I have kept the events in the base DataConnection without implementing these events. But BufferedDataConnection has got me covered nicely so far :)

I was thinking of adding a callback based api but favoured the event driven approach since I found it aligned well with what you guys were doing already.

Haven't made tests since I couldn't get them to work on my machine but I tried it with a personal example extensively, can give more examples if necessary, some of which reside in my continuation of https://github.com/peers/peerjs/pull/1075.

Thank you for your work and I hope this is useful to somebody as well

ProPablo avatar Sep 24 '23 09:09 ProPablo