Connect to Twitch

There are three things you need to do connect to a Twitch chat and listen for new messages.

  1. Instantiate a client with connection information.
  2. Call the connect() method on the client object.
  3. Execute a callback for message events.

This functionality has been written for you and lives in the src/twitch.js module.

The connection to a Twitch chat is handled inside a function that gets exported called twitchMessages(). The function takes a channel name, a duration in milliseconds, and a message callback. The channel is the string of the channel that will be monitored, for example tekken. The duration argument provides auto-exit functionality – the Twitch client will automatically disconnect after <duration> milliseconds. This prevents large AWS charges from racking up in case you accidentally leave the connection running. You can disable the auto-exit by passing in a duration of 0.

Update index.js

  1. In index.js, import the twitchMessages() function from src/twitch.js and call it with a channel, a dummy callback that does nothing, and a 10-second timeout. Copy the following and paste it into the index.js file. Then, make sure to save the file.

    // index.js
    const twitchMessages = require('./src/twitch');
    
    twitchMessages('stumptgamers', 10000);  // replace 'stumptgamers' with an active channel
    

    It’s OK to hardcode the channel and duration for now, but you should choose a channel that you know is currently active – this will make development and testing much easier.

  2. Now, run the script in the Terminal:

    node index.js
    

    After a few seconds, messages should start printing in the terminal. Your output will look something like the following:

    [14:28] info: Connecting to irc-ws.chat.twitch.tv on port 80..
    [14:28] info: Sending authentication to server..
    [14:28] info: Connected to server.
    [14:28] info: Executing command: JOIN #stumptgamers
    [14:28] info: Joined #stumptgamers
    [14:28] info: [#stumptgamers] <magicaking>: @Aherbash Hello! How's it goin?
    [14:28] info: [#stumptgamers] <hobbitkatie>: back to lurk. You all have fun.
    [14:28] info: Disconnecting from server..
    [14:28] info: Connection closed.
    ...
    

    You should see logs from the Twitch client whether or not message events get fired during the 10 seconds in which you run the listener. These debug logs are useful for confirming that the application is working as expected both locally and in ECS.

  3. Now, parameterize the channel and duration by accepting them as a command-line arguments. You can use yargs to accept --channel and --duration arguments. These will have -c and -d shortcuts respectively. Copy the following code into index.js:

    // index.js
    const twitchMessages = require('./src/twitch');
    
    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;
    
    twitchMessages(argv.channel, argv.duration);
    

    Don’t forget to save the file!

  4. Run the script again, but this time with channel and duration as arguments:

    node index.js -c stumptgamers -d 10000