Skip to main content

Socket Connection

In order to connect the user and the doctor there should be a channel that reflects the status update of the consultation, So we have the TBISocket to deal with the socket

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 this 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 an instance of the socket to deal with and you can get it 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 the instance.init()

await instance.init({
...socketParams,
onConnectionStateChange,
onError,
onEvent,
onSubscriptionSucceeded,
onSubscriptionError,
onDecryptionFailure,
onMemberAdded,
onMemberRemoved,
onSubscriptionCount,
});

These are the event handlers that could 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') {
let 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, and it is included in the consultation data object pusherChannel

await instance.subscribe({ channelName: pusherChannel });
await instance.connect();

Next Step (Move to Consultation)

On listening to the events, the event call-status reflects the status of the consultation, So you need to check the consultation data on event listener and call getConsultationInfo to check the config of the consultation and once the consultation status is in_progress and the config is ready you can move to the consultation screen

For Chat consultation the data will include the chatConfig object For Voip consultations the data will include the voipConfig object For Video consultations the data will include the videoConfig object

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;
apiKey?: string;
call_id?: string;
token?: string;
}