Chatroom tutorial part 2

Sending messages with the REST API

In this tutorial you are going to send random numbers to the chatroom from a Node.js script. Mind the tutorial is general enough to use with any language as there is very little that is Node.js specific.

Setup

Let's set up a basic Node.js project and install the popular request module for easy POST requests.

npm init
npm install request --save

Sending data to room

You're going to send a new random number to the chatroom every 10 seconds. Let's create a file called random.js that will make a POST request every 10 seconds. Body of the request will be a JSON message similar to the one you sent in the previous tutorial.

random.js
var request = require('request');

setInterval(function () {
  var message = {
    admin: true,
    name: 'Random Number Roller',
    content: 'New random number is ' + Math.floor(Math.random() * 100)
  }

  request({
    method: 'POST',
    url: 'https://api2.scaledrone.com/YOUR-CHANNEL-ID/general-chat/publish',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(message)
  }, function (error, response, body) {
    if (error) console.error(error);
  });
}, 10000);

Run this using node random.js messages will start popping up.

Extra style

For design's sake lets make the background of these messages yellowish. The admin: true parameter can be used to check whether a message is sent from the Randon Number script.

script.js
style.css
var drone = new ScaleDrone('your_channel_id');

drone.on('open', function (error) {
  if (error) return console.error(error);

  var room = drone.subscribe('general-chat');

  room.on('open', function (error) {
    if (error) return console.error(error);
    console.log('Connected to room');
  });
  
  room.on('data', addMessageToScreen);
});
      
function onSubmitForm(event) {
  var nameEl = document.querySelector('.input.name')
    , contentEl = document.querySelector('.input.content');

  if (nameEl.value && contentEl.value) {
    sendMessageToScaleDrone(nameEl.value, contentEl.value);
    contentEl.value = '';
  }
}
      
function sendMessageToScaleDrone(name, content) {
  drone.publish({
    room: 'general-chat',
    message: {
      name: name,
      content: content
    }
  });
}
      
function addMessageToScreen(message) {
  var div = document.createElement('div');
  div.innerHTML = '<b>' + message.name + '</b>: ' + message.content;
  div.classList.add('message');
  if (message.admin) {
    div.classList.add('admin');
  }
  document.querySelector('.text-area').appendChild(div);
}
body, input {
  font-size: 17px;
}

.message {
  margin: 15px 0;
}

.admin {
  background: #FFFBDC;
}

.input-area {
  display: flex;
}

.input {
  width: 100%;
  line-height: 40px;
  border: 2px solid #C3C3C3;
  border-radius: 8px;
  padding: 0 15px;
}

.input.name {
  flex-basis: 200px;
  flex-grow: 0;
  margin-right: 5px;
}

.input.content {
  flex-grow: 1;
  margin-right: 5px;
}

.input.button {
  flex-basis: 120px;
  flex-grow: 0;
}