Skip to content

Commit

Permalink
feat: add generic setConnectionInfo callback for dynamic connection d…
Browse files Browse the repository at this point in the history
…ata retrieval
  • Loading branch information
gabrielmatau79 committed Nov 13, 2024
1 parent c45ddd1 commit 9a06b73
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
10 changes: 5 additions & 5 deletions docs/message-pickup-repository-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ Registers a callback to handle `messagesReceived` events from the WebSocket serv

---

### `notificationToken(callback)`
### `setConnectionInfo(callback)`

Registers a callback to retrieve the `device_token` associated with a specific connection from mediator. This method is primarily used to provide the token when sending messages to the WebSocket server.
Registers a callback function to retrieve connection-specific information based on a given `connectionId`. This callback provides the `ConnectionInfo` object, which contains details such as the FCM notification token and maximum bytes allowed for receiving messages. This function is useful for dynamically fetching connection-related information whenever it is required by the client.

- **Parameters**:

- `callback`: A function that receives a `connectionId` as a parameter and returns a `Promise` that resolves to the device token or `undefined` if no token is available.
- `connectionId: string`: The connection ID for which to retrieve the token.
- `callback`: A function that takes a `connectionId` (string) and returns a `Promise` resolving to a `ConnectionInfo` object or `undefined` if no information is available for the given `connectionId`.
- `connectionId` (string): The ID of the connection for which to retrieve information.

- **Returns**: `void`
- **Returns**: `Promise<ConnectionInfo | undefined>`: The connection information, including: - `fcmNotificationToken` (optional, string): The FCM notification token for the specified connection. - `maxReceiveBytes` (optional, number): The maximum allowed bytes for receiving messages for this connection.

---

Expand Down
50 changes: 33 additions & 17 deletions packages/client/src/MessagePickupRepositoryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MessagesReceivedCallbackParams,
ExtendedTakeFromQueueOptions,
ExtendedAddMessageOptions,
ConnectionInfo,
} from './interfaces'
import {
AddMessageOptions,
Expand All @@ -22,13 +23,12 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
private client?: Client
private readonly logger = log
private messagesReceivedCallback: ((data: MessagesReceivedCallbackParams) => void) | null = null
private getDeviceTokenCallback?: (connectionId: string) => Promise<string | undefined>
private setConnectionInfoCallback?: (connectionId: string) => Promise<ConnectionInfo | undefined>
private readonly url: string
private readonly maxReceiveBytes?: number


constructor(options: { url: string; maxReceiveBytes?: number }) {
constructor(options: { url: string }) {
this.url = options.url
this.maxReceiveBytes = options.maxReceiveBytes
}

/**
Expand Down Expand Up @@ -105,24 +105,29 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
}

/**
* Sets the callback function to retrieve the device token.
* This function is called whenever a token is needed, allowing dynamic retrieval based on the connection ID.
* Sets the callback function to retrieve connection-specific information.
* This callback provides the `ConnectionInfo` object, containing details like
* the FCM notification token and max receive bytes, based on the given `connectionId`.
*
* @param callback - A function that receives a connectionId and returns a Promise resolving to a device token or undefined.
* @param {function} callback - A function that takes a `connectionId` as a parameter and returns
* a `Promise` that resolves to a `ConnectionInfo` object or `undefined` if no information is available.
*
* @example
* // Example of setting the callback to retrieve the device token
* // Example of setting the callback to retrieve connection-specific information
* const client = new MessagePickupRepositoryClient({ url: 'wss://example.com' });
*
* const getDeviceToken = async (connectionId: string) => {
* const getConnectionInfo = async (connectionId: string) => {
* const connectionRecord = await agent.connections.findById(connectionId);
* return connectionRecord?.getTag('device_token') as string | undefined;
* return {
* fcmNotificationToken: connectionRecord?.getTag('device_token') as string | undefined,
* maxReceiveBytes: config.messagePickupMaxReceiveBytes,
* };
* };
*
* client.notificationToken(getDeviceToken);
* client.setConnectionInfo(getConnectionInfo);
*/
notificationToken(callback: (connectionId: string) => Promise<string | undefined>): void {
this.getDeviceTokenCallback = callback
setConnectionInfo(callback: (connectionId: string) => Promise<ConnectionInfo | undefined>): void {
this.setConnectionInfoCallback = callback
}

/**
Expand All @@ -147,9 +152,15 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
try {
const client = this.checkClient()

const connectionInfo = this.setConnectionInfoCallback
? await this.setConnectionInfoCallback(params.connectionId)
: undefined

const maxReceiveBytes = connectionInfo?.maxReceiveBytes

// Add limitBytes to params if maxReceiveBytes is set
if (this.maxReceiveBytes) {
params = { ...params, limitBytes: this.maxReceiveBytes }
if (maxReceiveBytes) {
params = { ...params, limitBytes: maxReceiveBytes }
}

// Call the RPC method and store the result as 'unknown' type initially
Expand Down Expand Up @@ -212,8 +223,13 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository {
try {
const client = this.checkClient()

// Retrieve token using the callback, if set
params.token = this.getDeviceTokenCallback ? await this.getDeviceTokenCallback(params.connectionId) : undefined
// Retrieve connection information using the callback, if set
const connectionInfo = this.setConnectionInfoCallback
? await this.setConnectionInfoCallback(params.connectionId)
: undefined

// Set the token and max bytes from the connection info, if available
params.token = connectionInfo?.fcmNotificationToken

// Call the 'addMessage' RPC method on the WebSocket server
const result: unknown = await client.call('addMessage', params, 2000)
Expand Down
5 changes: 5 additions & 0 deletions packages/client/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export interface ExtendedTakeFromQueueOptions extends TakeFromQueueOptions {
export interface ExtendedAddMessageOptions extends AddMessageOptions {
token?: string
}

export interface ConnectionInfo {
fcmNotificationToken?: string
maxReceiveBytes?: number
}

0 comments on commit 9a06b73

Please sign in to comment.