Simple Instant messaging client

Load Library

The base of the script is the same as before. It begins with importing ApiRTC, and we also include the twitter bootstrap library here, to show a simple chat box :


Then we load the html chat box, but it will be hidden at first.


We then create an IMClient with ApiRTC, after binding the “receiveIMMessage” event to our custom handler.

When the “createBox” button is clicked, we want the box to pop up, and to be able to enter in communication with the remote client.

With our IMClient created and our box open, we can now send messages to the remote client. After the users enters a message, the following function is executed to send it.

A symmetrical behavior is then expected of the remote client, which has also created it’s IMClient, and has registered an event handler with the event “receiveIMMessage”. The receiveIMMessageHandler function initializes the IMClient if necessary, and then displays the message received.

The communication is now established between the two clients, and they can continue exchanging messages. To go further, you can also check our group chat example.