# Chat message

In order to send and receive chat messages, necessary object declarations and chat window functions must be prepared.
If you are not ready, let's learn how to create it. Go to preparations

# Send message

channel.sendMessage({
    message: "message to be sent",
    mimeType: "text"
}, function(err) {
    // null on success, err object return on error ({code: -1, type: "TIMEOUT", message: "Timed out after waiting 30000(ms) ..."})
    if(err) openError("Message transmission failed.")
})
1
2
3
4
5
6
7
  • Parameter value

    Value Identifier Description
    message String Message to be sent
    mimeType String message type (text: plain text, emoji: emoji)
  • Result value

    Value Identifier Description
    code String Error code
    message String Error message

# Code to be applied to the project

    # New message event

    channel.onNotifyMessage = function(event) {
        // {"nickName":"Kildong Hong","roomId":"e7works","clientKey":"user unique key","message":"message to be sent","mimeType":"text","messageDt" :"20200826160708","messageCount":4979,"grade":"user"}
        console.log(event)
    };
    
    1
    2
    3
    4
    • Push result value

      Value Identifier Description
      nickName String Nickname of the user entering the chat room that sent the message
      roomId String Channel Key issued after joining CMS
      clientKey String Unique key for setting the connected terminal that sent the message
      message String Sent message
      mimeType String message type (text: plain text, emoji: emoji)
      messageDt String Send date
      messageCount String Number of chat room messages sent
      grade String Level of the user who sent the message (Check in the User Rating Table )

    # Code to be applied to the project

    channel.onNotifyMessage = function(event) {
        write(event)
    };
    
    1
    2
    3

    # Send a message to a specific user

    channel.sendWhisper({
        message: "message to be sent (whisper)",
        mimeType: "text",
        receivedClientKey: "user unique key"
    }, function(err, msg) {
        if (err) {
            // null on success, return err object on error ({"code":10107,"type":"RECIPIENT_FAILURE","message":"USER_NOT_EXISTED"})
            return console.log(err)
        }
        // Return object on success {"nickName":"vchat","roomId":"e7works","clientKey":"2a66d6a3","message":"hello","receivedClientKey":"chatSample","mimeType" :"text","messageDt":"20200831122540","messageCount":5852,"grade":"user","receivedNickName":"Kildong Hong"}
        console.log(msg)
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    • Parameter value

      Value Identifier Description
      message String Message to be sent
      mimeType String message type (text: plain text, emoji: emoji)
      receivedClientKey String Unique key for setting the connected terminal of the receiving user
    • Result value

      • err
      Value Identifier Description
      code String Error code
      message String Error message
      • msg
      Value Identifier Description
      roomId String Channel Key issued after joining CMS
      nickName String The nickname of the user entering the chat room
      clientKey String Unique key for setting the connected terminal that sent the message
      message Date Sent message
      mimeType String message type (text: plain text, emoji: emoji)
      messageDt String Send date
      messageCount String Number of chat room messages sent
      receivedNickName String Nickname of the user receiving the message
      receivedClientKey String Unique key for setting the connected terminal to receive the message
      grade String Level of the user who sent the message (Check in the User Rating Table )

    # Code to be applied to the project

    var popupLayer = $('div.popupLayer');
    var whisperLayer = $('ul.popup_content li:nth-child(1)', $('div.popupLayer'));
    popupLayer.close = function() {
        $('#whisper input[type=text][name=message]', whisperLayer).val('');
        $("#whisper", this).hide();
        $(this).hide();
    }
    $('button', whisperLayer).click(function(e) {
        channel.sendWhisper({
            message: $('input[type=text][name=message]', whisperLayer).val(),
            receivedClientKey: popupLayer.data()['clientKey']
        }, function(err, msg) {
            if (err)
                return openError(err.message);
            write(msg,'whisperto')
        })
        e.preventDefault();
        popupLayer.close();
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    # Personal Whisper Message Event

    channel.onPersonalWhisper = function(event) {
        // {"nickName":"Kildong Hong","roomId":"e7works","clientKey":"user unique key","message":"message to be sent (whisper)","receivedClientKey":"user unique key ","mimeType":"text","messageDt":"20200826160933","messageCount":4981,"grade":"user"}
        console.log('onPersonalWhisper', event)
    }
    
    1
    2
    3
    4
    • Push result value

      Value Identifier Description
      roomId String Channel Key issued after joining CMS
      nickName String The nickname of the user entering the chat room
      clientKey String Unique key for setting the connected terminal that sent the message
      message Date Sent message
      mimeType String message type (text: plain text, emoji: emoji)
      messageDt String Send date
      messageCount String Number of chat room messages sent
      receivedClientKey String Unique key for setting the connected terminal to receive the message
      grade String Level of the user who sent the message (Check in the User Rating Table )

    # Code to be applied to the project

    channel.onPersonalWhisper = function(event) {
        write(event,'whisper')
    }
    
    1
    2
    3

    # full code

    message event full code
    $(function() {
        // Enter the input window
        $('div.bottom div.emoji a').click(function() {
            channel.sendMessage({
                message: $(this).text(),
                mimeType:'emoji'
            });
        });
        // click button
        $('#sendCounter').click(function(e) {
            channel.sendMessage({
                message: $('#content').text(),
                mimeType: "text"
            })
            $('#content').text('')
        })
        // Emoji button
        $('#content').keydown(function(e) {
            if (e.keyCode == 13) (
                e.preventDefault();
                channel.sendMessage({
                    message: $(this).text(),
                    mimeType: "text"
                })
                $(this).text('');
            }
        })
        // Send message to specific user
        var popupLayer = $('div.popupLayer');
        var whisperLayer = $('ul.popup_content li:nth-child(1)', $('div.popupLayer'));
        popupLayer.close = function() {
            $('#whisper input[type=text][name=message]', whisperLayer).val('');
            $("#whisper", this).hide();
            $(this).hide();
        }
        $('button', whisperLayer).click(function(e) {
            channel.sendWhisper({
                message: $('input[type=text][name=message]', whisperLayer).val(),
                receivedClientKey: popupLayer.data()['clientKey']
            }, function(err, msg) {
                if (err)
                    return openError(err.message);
                write(msg,'whisperto')
            })
            e.preventDefault();
            popupLayer.close();
        });
    
    })
    // The chatInit() part is executed after joinRoom succeeds in login.js
    function chatInit() {
        // New message event
        channel.onNotifyMessage = function(event) {
            write(event)
        };
        // Personal whisper message event
        channel.onPersonalWhisper = function(event) {
            write(event,'whisper')
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    Copyright © 2020 | E7Works.Inc & JOYTUNE Corp.