WebSocket Connection

Manu da Silva Updated by Manu da Silva

The WebSocket Server uses socketCluster on version 9

  1. Establishing the connection to the WebSocket Server

    To start a socket connection you will need to do the following:
     const socket = socketCluster.connect(options);
    Where options is an object with the following structure:
    const options = {
    hostname: <hostName>, // Socket Server URL
    query:{
    channelUUID: <channelUuid>, // Push ChannelUuid
    hostApi:<hostApi> // Push hostApi URL
    }
    }
    After that, the WebSocket Server will be listening to future events, such as registerUser and sendMessageToChannel that will be emitted from the declared socket variable.
  2. Registering the user

    Before exchanging messages, the user must be registered, user registration should be the first thing to be done after establishing the connection.
    For that, an event registerUser should be emitted, without arguments, and with a callback function. The callback function will handle the following response from the WebSocket Server.
    Example:
    socket.emit('registerUser', {}, (_response) => {
    const res = JSON.parse(_response);
    console.log(res.urn);
    });
    Response Example:
    { 
    urn: "xxxxxxxxxxx-xxxxxxxx"
    }
    The urn field, represents the current Socket Session Id.
  3. Receiving Messages from the WebSocket Server

    After user registration, the current Socket Instance must be subscribed to handle messages from the WebSocket Server. Example on how to subscribe:
    socket.subscribe(sessionUrn, (data) => {
    if(data.to === sessionUrn) {//check msg destination
    console.log(data);
    }
    });
    Where data is the response that our platform sent.
  4. Sending Messages to the WebSocket Server

    To send a message to the WebSocket Server, the sendMessageToChannel must be emitted, with the following arguments:
    text: The user message text.
    userUrn: The session urn received when registering the user.
    Example:
    socket.emit("sendMessageToChannel", {
    text: "Hello",
    userUrn: sessionUrn
    })

How did we do?

How a SMS Integration Works

General API concepts and Integrations

Contact