Skip to content

Commit

Permalink
feat: get registration state from the gateway (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha authored Aug 1, 2024
1 parent 14c1287 commit c5af08b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ client.newCall({
});
```

### Getting the Registration State

To retrieve the registration state from the server gateway you can
use `client.getIsRegistered` method

```js
client.getIsRegistered().then(isRegistered => {...})
```

---

## Examples
Expand Down
11 changes: 11 additions & 0 deletions packages/js/src/Modules/Verto/BaseSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
register,
trigger,
} from './services/Handler';
import { RegisterAgent } from './services/RegisterAgent';
import { SwEvent } from './util/constants';
import { isFunction, isValidOptions, randomInt } from './util/helpers';
import { BroadcastParams, IVertoOptions } from './util/interfaces';
Expand Down Expand Up @@ -36,6 +37,7 @@ export default abstract class BaseSession {

private _executeQueue: { resolve?: Function; msg: any }[] = [];
private _pong: boolean;
private registerAgent: RegisterAgent;

constructor(public options: IVertoOptions) {
if (!this.validateOptions()) {
Expand All @@ -48,6 +50,7 @@ export default abstract class BaseSession {

this._attachListeners();
this.connection = new Connection(this);
this.registerAgent = new RegisterAgent(this);
}

get __logger(): log.Logger {
Expand Down Expand Up @@ -412,4 +415,12 @@ export default abstract class BaseSession {
public hasAutoReconnect() {
return this._autoReconnect;
}

/**
* Get the registration state of the client
* @return Promise<boolean>
*/
public getIsRegistered() {
return this.registerAgent.getIsRegistered();
}
}
39 changes: 39 additions & 0 deletions packages/js/src/Modules/Verto/services/RegisterAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import BaseSession from '../BaseSession';
import { Gateway } from '../messages/verto/Gateway';
import {
deferredPromise,
DeferredPromise,
getGatewayState,
} from '../util/helpers';
import { GatewayStateType } from '../webrtc/constants';

export class RegisterAgent {
private session: BaseSession;
private gatewayStateTask: DeferredPromise<GatewayStateType | ''>;
private pendingRequestId: string | null = null;

constructor(session: BaseSession) {
this.session = session;
this.gatewayStateTask = deferredPromise<GatewayStateType>({});
this.session.on('telnyx.socket.message', this.onSocketMessage);
}

public onSocketMessage = async (response) => {
if (response.id === this.pendingRequestId) {
this.gatewayStateTask.resolve(getGatewayState(response));
}
};

public getIsRegistered = async () => {
const message = new Gateway();
this.pendingRequestId = message.request.id;
this.gatewayStateTask = deferredPromise<GatewayStateType>({});

this.session.execute(message);
const state = await this.gatewayStateTask.promise;
if (!state) {
return false;
}
return [GatewayStateType.REGISTER, GatewayStateType.REGED].includes(state);
};
}

0 comments on commit c5af08b

Please sign in to comment.