Skip to content

Commit b5c4fc4

Browse files
Allow custom app logic to trigger when a websocket connects/reconnects
Signed-off-by: Peter Broadhurst <[email protected]>
1 parent 6fa9555 commit b5c4fc4

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

lib/firefly.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
FireFlyDeleteOptions,
8080
FireFlyTokenApprovalFilter,
8181
FireFlyTokenApprovalResponse,
82+
FireFlyWebSocketConnectCallback,
8283
} from './interfaces';
8384
import { FireFlyWebSocket, FireFlyWebSocketCallback } from './websocket';
8485
import HttpBase, { mapConfig } from './http';
@@ -598,6 +599,7 @@ export default class FireFly extends HttpBase {
598599
subscriptions: string | string[] | FireFlySubscriptionBase,
599600
callback: FireFlyWebSocketCallback,
600601
socketOptions?: WebSocket.ClientOptions | http.ClientRequestArgs,
602+
afterConnect?: FireFlyWebSocketConnectCallback,
601603
): FireFlyWebSocket {
602604
const options: FireFlyWebSocketOptions = {
603605
host: this.options.websocket.host,
@@ -609,6 +611,7 @@ export default class FireFly extends HttpBase {
609611
reconnectDelay: this.options.websocket.reconnectDelay,
610612
heartbeatInterval: this.options.websocket.heartbeatInterval,
611613
socketOptions: socketOptions,
614+
afterConnect: afterConnect,
612615
};
613616

614617
const handler: FireFlyWebSocketCallback = (socket, event) => {

lib/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export interface FireFlyOptions extends FireFlyOptionsInput {
6161
};
6262
}
6363

64+
export interface FireFlyWebSocketSender {
65+
send: (json: JSON) => void;
66+
}
67+
68+
export interface FireFlyWebSocketConnectCallback {
69+
(sender: FireFlyWebSocketSender): void | Promise<void>;
70+
}
71+
6472
export interface FireFlyWebSocketOptions {
6573
host: string;
6674
namespace: string;
@@ -72,6 +80,7 @@ export interface FireFlyWebSocketOptions {
7280
reconnectDelay: number;
7381
heartbeatInterval: number;
7482
socketOptions?: WebSocket.ClientOptions | http.ClientRequestArgs;
83+
afterConnect?: FireFlyWebSocketConnectCallback;
7584
}
7685

7786
// Namespace

lib/websocket.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export class FireFlyWebSocket {
8383
);
8484
this.logger.log(`Started listening on subscription ${this.options.namespace}:${name}`);
8585
}
86+
if (this.options?.afterConnect !== undefined) {
87+
this.options.afterConnect(this);
88+
}
8689
})
8790
.on('error', (err) => {
8891
this.logger.error('Error', err.stack);
@@ -156,6 +159,12 @@ export class FireFlyWebSocket {
156159
}
157160
}
158161

162+
send(json: JSON) {
163+
if (this.socket !== undefined) {
164+
this.socket.send(JSON.stringify(json));
165+
}
166+
}
167+
159168
ack(event: FireFlyEventDelivery) {
160169
if (this.socket !== undefined && event.id !== undefined) {
161170
this.socket.send(

0 commit comments

Comments
 (0)