voximplant

How to use Voximplant text chatting together with Web client?


I'm trying to find out how to create chat between two users. I can't find documentation how to configure control panel and how to write client to start chatting.


Solution

  • You need to do the following setup in the Voximplant Control Panel:

    1. create a Voximplant application
    2. create several Voximplant users in the Voximplant application

    On the web client side do the initial setup

    1. Install the Voximplant Web SDK. Ensure that you use the latest SDK version
    2. Initialize the SDK, connect and login a Voximplant user (created in the Voximplant Control Panel)

    To send a message from user1 to user2, you need:

    1. on the user1 side get the user2's imId using VoxImplant.Messaging.Messenger.getUser API
    // let's assume user2's Voximplant username is user2@app.account.voximplant.com, 
    // where:
    // - app is your Voximplant application name 
    // - account is your Voximplant account name
    let messenger = VoxImplant.Messaging.getInstance();
    let userEvent = await messenger.getUser("user2@app.account");
    let user2Id = userEvent.user.userId;
    
    1. create a conversation between user1 and user2 via VoxImplant.Messaging.Messenger.createConversation API
    let participant = {userId: user2Id};
    let conversationEvent = await messenger.createConversation([participant]);
    let conversation = conversationEvent.conversation;
    
    1. send a message to user2 via VoxImplant.Messaging.Conversation.sendMessage API
    let messageEvent = await conversation.sendMessage("text message");
    // to obtain a sent message from the event
    let message = messageEvent.message;
    

    To receive messages and other messaging events, you need to subscribe on MessengerEvents. The most important for this case are:

    1. CreateConversationEvent
    2. SendMessageEvent
    messenger.on(VoxImplant.Messaging.MessengerEvents.CreateConversation, 
      (conversationEvent) => { 
        // user2 receives this event after user1 has created a conversation
        // obtain the conversation instance from `conversationEvent` to send messages
        // from user2 to user
        let conversation = conversationEvent.conversation;
    });
    
    messenger.on(VoxImplant.Messaging.MessengerEvents.SendMessage, 
      (messageEvent) => {
        // user2 receives this event after user1 has sent a message
        // to get the text of the message
        let textMessage = messageEvent.message.text;
    });