Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add bidirectional communication to exchange texts #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions flottform/forms/src/default-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@ const handleTextInputStates = ({
if (id) {
flottformItem.setAttribute('id', id);
}
flottformTextInputHost.on('done', (message: string) => {
statusInformation.innerHTML = onSuccessText ?? `✨ You have succesfully submitted your message`;
flottformTextInputHost.on('text-received', (message: string) => {
statusInformation.innerHTML =
onSuccessText ?? `✨ You have succesfully submitted your message: ${message}`;
statusInformation.appendChild(refreshChannelButton);
flottformItem.replaceChildren(statusInformation);
});
Expand Down
49 changes: 37 additions & 12 deletions flottform/forms/src/flottform-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Listeners = {
'connecting-to-host': [];
connected: [];
'connection-impossible': [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'receiving-data': [e: any];
done: [];
disconnected: [];
error: [e: string];
Expand Down Expand Up @@ -96,15 +98,8 @@ export class FlottformChannelClient extends EventEmitter<Listeners> {
this.logger.info(`ondatachannel: ${e.channel}`);
this.changeState('connected');
this.dataChannel = e.channel;
// Set the maximum amount of data waiting inside the datachannel's buffer
this.dataChannel.bufferedAmountLowThreshold = this.BUFFER_THRESHOLD;
// Set the listener to listen then emit an event when the buffer has more space available and can be used to send more data
this.dataChannel.onbufferedamountlow = () => {
this.emit('bufferedamountlow');
};
this.dataChannel.onopen = (e) => {
this.logger.info(`ondatachannel - onopen: ${e.type}`);
};
this.configureDataChannel();
this.setupDataChannelListener();
};

this.changeState('sending-client-info');
Expand All @@ -122,11 +117,41 @@ export class FlottformChannelClient extends EventEmitter<Listeners> {
this.changeState('disconnected');
};

// sendData = (data: string | Blob | ArrayBuffer | ArrayBufferView) => {
private setupDataChannelListener = () => {
if (!this.dataChannel) {
this.changeState(
'error',
'dataChannel is not defined. Unable to setup the listeners for the data channel'
);
return;
}

this.dataChannel.onmessage = (e) => {
// Handling the incoming data from the Host depends on the use case.
this.emit('receiving-data', e);
};
};

private configureDataChannel = () => {
if (!this.dataChannel) {
this.changeState('error', 'dataChannel is not defined! Unable to configure it!');
return;
}
// Set the maximum amount of data waiting inside the datachannel's buffer
this.dataChannel.bufferedAmountLowThreshold = this.BUFFER_THRESHOLD;
// Set the listener to listen then emit an event when the buffer has more space available and can be used to send more data
this.dataChannel.onbufferedamountlow = () => {
this.emit('bufferedamountlow');
};
this.dataChannel.onopen = (e) => {
this.logger.info(`ondatachannel - onopen: ${e.type}`);
};
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendData = (data: any) => {
if (this.dataChannel == null) {
this.changeState('error', 'dataChannel is null. Unable to send the file to the Host!');
if (!this.dataChannel) {
this.changeState('error', 'dataChannel is not defined! Unable to send the file to the Host!');
return;
} else if (!this.canSendMoreData()) {
this.logger.warn('Data channel is full! Cannot send data at the moment');
Expand Down
41 changes: 40 additions & 1 deletion flottform/forms/src/flottform-channel-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class FlottformChannelHost extends EventEmitter<FlottformEventMap> {
private openPeerConnection: RTCPeerConnection | null = null;
private dataChannel: RTCDataChannel | null = null;
private pollForIceTimer: NodeJS.Timeout | number | null = null;
private BUFFER_THRESHOLD = 128 * 1024; // 128KB buffer threshold (maximum of 4 chunks in the buffer waiting to be sent over the network)

constructor({
flottformApi,
Expand Down Expand Up @@ -70,6 +71,10 @@ export class FlottformChannelHost extends EventEmitter<FlottformEventMap> {
this.openPeerConnection = new RTCPeerConnection(this.rtcConfiguration);

this.dataChannel = this.createDataChannel();
if (this.dataChannel) {
this.configureDataChannel();
this.setupDataChannelListener();
}

const session = await this.openPeerConnection.createOffer();
await this.openPeerConnection.setLocalDescription(session);
Expand Down Expand Up @@ -120,6 +125,40 @@ export class FlottformChannelHost extends EventEmitter<FlottformEventMap> {
};
};

private configureDataChannel = () => {
if (this.dataChannel == null) {
this.changeState('error', 'dataChannel is null. Unable to setup the configure it!');
return;
}
// Set the maximum amount of data waiting inside the datachannel's buffer
this.dataChannel.bufferedAmountLowThreshold = this.BUFFER_THRESHOLD;
// Set the listener to listen then emit an event when the buffer has more space available and can be used to send more data
this.dataChannel.onbufferedamountlow = () => {
this.emit('bufferedamountlow');
};
this.dataChannel.onopen = (e) => {
this.logger.info(`ondatachannel - onopen: ${e.type}`);
};
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendData = (data: any) => {
if (this.dataChannel == null) {
this.changeState('error', 'dataChannel is null. Unable to send the data to the Client!');
return;
} else if (!this.canSendMoreData()) {
this.logger.warn('Data channel is full! Cannot send data at the moment');
return;
}
this.dataChannel.send(data);
};

canSendMoreData = () => {
return (
this.dataChannel &&
this.dataChannel.bufferedAmount < this.dataChannel.bufferedAmountLowThreshold
);
};

private setupHostIceGathering = (
putHostInfoUrl: string,
hostKey: string,
Expand Down Expand Up @@ -279,7 +318,7 @@ export class FlottformChannelHost extends EventEmitter<FlottformEventMap> {

this.dataChannel.onerror = (e) => {
this.logger.log('channel.onerror', e);
this.changeState('error', { message: 'file-transfer' });
this.changeState('error', { message: e.error.message });
};
};

Expand Down
22 changes: 16 additions & 6 deletions flottform/forms/src/flottform-text-input-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { FlottformChannelClient } from './flottform-channel-client';
import { EventEmitter, Logger, POLL_TIME_IN_MS } from './internal';

type Listeners = {
init: [];
connected: [];
'webrtc:connection-impossible': [];
sending: []; // Emitted to signal the start of sending the file(s)
done: [];
'text-transferred': [text: string]; // Emitted to signal the transfer of one text TO the Host.
'text-received': [text: string]; // Emitted to signal the reception of one text FROM the Host.
disconnected: [];
error: [e: string];
};
Expand Down Expand Up @@ -46,13 +47,19 @@ export class FlottformTextInputClient extends EventEmitter<Listeners> {

sendText = (text: string) => {
// For now, I didn't handle very large texts since for most use cases the text won't exceed the size of 1 chunk ( 16KB )
this.emit('sending');
this.channel?.sendData(text);
this.emit('done');
this.emit('text-transferred', text);
};

private handleIncomingData = (e: MessageEvent) => {
// We suppose that the data received is small enough to be all included in 1 message
this.emit('text-received', e.data);
};

private registerListeners = () => {
this.channel?.on('init', () => {});
this.channel?.on('init', () => {
this.emit('init');
});
this.channel?.on('retrieving-info-from-endpoint', () => {});
this.channel?.on('sending-client-info', () => {});
this.channel?.on('connecting-to-host', () => {});
Expand All @@ -62,8 +69,11 @@ export class FlottformTextInputClient extends EventEmitter<Listeners> {
this.channel?.on('connection-impossible', () => {
this.emit('webrtc:connection-impossible');
});
this.channel?.on('receiving-data', (e) => {
this.handleIncomingData(e);
});
this.channel?.on('done', () => {
this.emit('done');
//this.emit('done');
});
this.channel?.on('disconnected', () => {
this.emit('disconnected');
Expand Down
13 changes: 9 additions & 4 deletions flottform/forms/src/flottform-text-input-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { FlottformChannelHost } from './flottform-channel-host';
import { BaseInputHost, BaseListeners, Logger, POLL_TIME_IN_MS } from './internal';

type Listeners = BaseListeners & {
done: [data: string];
'text-transferred': [text: string]; // Emitted to signal the transfer of one text TO the Client.
'text-received': [text: string]; // Emitted to signal the reception of one text FROM the Client.
'webrtc:waiting-for-text': [];
receive: [];
'webrtc:waiting-for-data': [];
};

Expand Down Expand Up @@ -67,10 +67,15 @@ export class FlottformTextInputHost extends BaseInputHost<Listeners> {
return this.qrCode;
};

sendText = (text: string) => {
// For now, I didn't handle very large texts since for most use cases the text won't exceed the size of 1 chunk ( 16KB )
this.channel?.sendData(text);
this.emit('text-transferred', text);
};

private handleIncomingData = (e: MessageEvent) => {
this.emit('receive');
// We suppose that the data received is small enough to be all included in 1 message
this.emit('done', e.data);
this.emit('text-received', e.data);
if (this.inputField) {
this.inputField.value = e.data;
const event = new Event('change');
Expand Down
1 change: 1 addition & 0 deletions flottform/forms/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export type FlottformEventMap = {
error: [error: Error];
connected: [];
disconnected: [];
bufferedamountlow: [];
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
4 changes: 4 additions & 0 deletions servers/demo/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export const createExpenseReportClientUrl = async ({ endpointId }: { endpointId:
export const createDeExpenseReportClientUrl = async ({ endpointId }: { endpointId: string }) => {
return `${window.location.origin}${base}/belegeinreichung-client/${endpointId}`;
};

export const createFlottformMessagingClientUrl = async ({ endpointId }: { endpointId: string }) => {
return `${window.location.origin}${base}/flottform-messaging-client/${endpointId}`;
};
5 changes: 5 additions & 0 deletions servers/demo/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,10 @@
title="Customized default UI"
description="See how you can tailor Flottform's default UI to better match your design, while still retaining all the powerful features of the original interface. This demo lets you explore how easy it is to adapt the default elements to fit seamlessly with your brand's style."
/>
<DemoLink
href="{base}/flottform-messaging"
title="Flottform Messaging"
description="See how you can use Flottform to build a simple messaging application!"
/>
</div>
</section>
Loading