Send sentiment to CloudWatch

The last thing you need to do to monitor the sentiment of a Twitch chat is send the scores into CloudWatch as a custom metric. The putMetricData() method allows you to send in values that can be plotted on a time series graph.

This call is managed in the src/cloudwatch.js module.

The goal of the tutorial is to keep an eye on negative sentiment. So the metrics that you send to CloudWatch should be confidence scores for all of the negative predictions. Accordingly, the MetricName will be ‘NegativeSentiment’ for each metric. The function also adds the name of the channel as a dimension. All of the metrics you write will be organized in the ‘TwitchSentiment’ namespace.

In the index.js script, you need to import the sendNegativeSentiment() function and write the logic to parse the response from the DetectSentiment API and convert it into a PutMetricData API call with the negative scores.

Update index.js

  1. Make the following changes to index.js (don’t forget to save):

    // index.js
    const twitchMessages = require('./src/twitch');
    const DetectSentiment = require('./src/comprehend');
    const sendNegativeSentiment = require('./src/cloudwatch');
    
    const argv = require('yargs')
        .options({
            'channel': {
                alias: 'c',
                describe: 'Twitch channel',
                demandOption: true,
            },
            'duration': {
              alias: 'd',
              describe: 'Monitoring time (ms)',
              type: 'number',
              default: 300000,
            },
        }).argv;
    
    const detectSentiment = new DetectSentiment(data => {
      const { ResultList } = data;
      const negatives = ResultList
        .map(negative => negative.SentimentScore.Negative);
      sendNegativeSentiment(argv.channel, negatives);
    });
    twitchMessages(
      argv.channel,
      argv.duration,
      (_, __, message) => detectSentiment.addMessage(message),
    );
    

    The script now listens for new Twitch messages, checks sentiment with Comprehend after every 20 messages, extracts the negative confidence for each message and logs those confidence scores to CloudWatch as metrics.

  2. Call the script again:

    AWS_REGION=us-east-1 node index.js -c esl_csgo
    

    Remember you can replace esl_csgo with any channel you want – it’s a good idea to pick an active channel. Negative sentiment is now getting logged to CloudWatch. It can take up to 15 minutes before the new namespace shows up in the dashboard. Let the program run for the full five minutes.

View the metrics in CloudWatch

After a few minutes, you will be able to see the real-time negative sentiment on a time series graph in CloudWatch.

  1. Navigate to Amazon CloudWatch console.
  2. In the menu on the left, choose Metrics.
  3. Under the All metrics tab, in the Custom Namespaces section, choose TwitchSentiment.
  4. Click on the Channel link. Select the channel for which you have been logging sentiment.
  5. Under the Graphed metrics tab, in the Period column, choose 1 Minute.
  6. Under the Graph options tab, in the Left Y Axis section set Min and Max at 0.0 and 1.0 respectively. Then uncheck the Units box.
  7. Above the graph on the left side, click on the pencil icon to change the name of the graph. Call it Negative Sentiment - <channel name>.
  8. Above the graph on the right side, there are options to limit the time window. Choose custom and then 30 minutes underneath the Relative tab.

Your graph should look something like this: