Socket Connection
In order to connect the user and the doctor
there should be a channel that reflects the status update of the consultation.
We use TBISocket to deal with the socket connection.
SocketParams Interface
For initiating the socket, the SocketParams data object is needed.
It is already included in the consultation data.
interface SocketParams {
apiKey?: string;
cluster?: string;
authEndpoint?: string;
}
Consultation Data
The ConsultationType object will have the SocketParams object included,
so the process of initiating the socket is done by passing the SocketParams object.
Here is the consultation data once the consultation is created:
const createdConsultation = {
id: 123,
userId: 999999999,
question: "I want to consult a doctor on ...",
medium: "chat",
status: "new",
pusherChannel: "channel_name",
socketParams: {
apiKey: "key",
cluster: "cluster",
authEndpoint: "endpoint",
},
chatConfig: {},
voipConfig: {},
videoConfig: {},
// ... Other Data
};
TBISocket Instance
There is a singleton instance of the socket that you can get using getInstance:
import { TBISocket, TBISocketEvent, TBISocketMember } from 'react-native-altibbi';
const instance = TBISocket.getInstance();
The TBISocketEvent is used to decode the eventData object that will be used in the event handling.
The TBISocketMember is used to decode the member object that will be used in the event handling.
Initiating the Socket
For initiating the socket you need the SocketParams object
and the event handlers to be declared and passed to instance.init():
await instance.init({
...socketParams,
onConnectionStateChange,
onError,
onEvent,
onSubscriptionSucceeded,
onSubscriptionError,
onDecryptionFailure,
onMemberAdded,
onMemberRemoved,
onSubscriptionCount,
});
These are the event handlers that can be used:
const onConnectionStateChange = (
currentState: string,
previousState: string
) => {
// Handle Connection State Changes
};
const onError = (message: string, code: Number, error: any) => {
// Handle Errors
};
/**
* This is the event handler that listens to the status of the consultation
* and indicates the start of a consultation by checking the call-status event
*/
const onEvent = (eventData: TBISocketEvent) => {
if (eventData && eventData.eventName === 'call-status') {
const event = JSON.parse(eventData.data);
if (event.status === 'in_progress') {
// The consultation starts
}
}
};
const onSubscriptionSucceeded = (channelName: string, data: any) => {
// Handle Subscription Success
};
const onSubscriptionError = (
channelName: string,
message: string,
e: any
) => {
// Handle Subscription Errors
};
const onMemberAdded = (channelName: string, member: TBISocketMember) => {
// Handle Added Members
};
const onMemberRemoved = (channelName: string, member: TBISocketMember) => {
// Handle Removed Members
};
const onSubscriptionCount = (
channelName: string,
subscriptionCount: Number
) => {
// Handle Subscription Count
};
Subscribe and Connect the Socket
In order to subscribe the socket, the channel name is needed.
It is included in the consultation data object as pusherChannel:
await instance.subscribe({ channelName: pusherChannel });
await instance.connect();
Next Step (Move to Consultation)
On listening to the events, the call-status event reflects the status of the consultation.
You need to check the consultation data on the event listener and call getConsultationInfo to check the config of the consultation.
Once the consultation status is in_progress and the config is ready, you can move to the consultation screen.
- For
Chatconsultations the data will include thechatConfigobject - For
Voipconsultations the data will include thevoipConfigobject - For
Videoconsultations the data will include thevideoConfigobject
Config Interfaces
interface ChatConfig {
id?: number;
consultation_id?: number;
group_id?: string;
chat_user_id?: string;
app_id?: string;
chat_user_token?: string;
}
// For both voipConfig and videoConfig
interface VoipConfig {
id?: number;
consultation_id?: number;
api_key?: string;
call_id?: string;
token?: string;
created_at?: string;
updated_at?: string;
}