Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to build a Twitch chat overlay with HTML, CSS and JS
Anneta Wamono
Anneta Wamono

Posted on • Edited on

     

How to build a Twitch chat overlay with HTML, CSS and JS

Like me, you may have started watching more Twitch streams in the last two years. One of the things that makes a Twitch stream fun and interactive are the overlays that react to the chat's input. So by the end of this tutorial, you'll have a simple chat overlay to use in your streams. You can take this tutorial and build on it to make more complex and interactive overlays.

Prerequisites

If you are a beginner at web dev, this is for you. To complete this tutorial, you'll need to know some HTML, CSS, JS. We'll needComfyJS to complete the project.

Project requirements:

Table of contents

  1. Setting up the project
  2. Connecting to Twitch chat
  3. Sending messages to the DOM
  4. Styling
  5. Adding animation
  6. Adding different styles based on roles

Setting up the project

We'll be writing our code in a single HTML file. Create a chat.html file and add the following:

<htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Twitch chat</title></head><body><divid="chat"><ul></ul></div></body></html>
Enter fullscreen modeExit fullscreen mode

We'll be putting our Twitch messages in the<ul> block as<li> elements. Next, let's integrate with Twitch.

Connecting to our Twitch chat

We'll be using ComfyJS to integrate with our Twitch chat. It's a very easy-to-use library, and it only takes a few lines of code to connect with our chat. Let's add this code to our<head>:

<scriptsrc="https://cdn.jsdelivr.net/npm/comfy.js@latest/dist/comfy.min.js"></script>
Enter fullscreen modeExit fullscreen mode

Next, we need to add a<script> tag below our<body> tag and add the following:

ComfyJS.onChat=(user,message,flags,self,extra)=>{console.log(user,message);}ComfyJS.Init("YourTwitchName");
Enter fullscreen modeExit fullscreen mode

ComfyJS.Init() lets us connect to our Twitch account, so remember to replaceYourTwitchName with your channel name. We want to test if we're connected to our chat, and we can do that by going to ourTwitch Creator Dashboard and selecting Stream Manager. There, you'll see a "My Chat" window where you can send messages to test your code. If you open chat.html in your browser and open your dev tools, you'll see the messages appear.

Sending Hello There to Twitch chat and it appears in the console

Sending messages to the DOM

So now that we're receiving messages from our chat let's start sending them to our DOM. We'll adjust our code in our<script> to the following:

varchat=document.querySelector("#chat>ul");ComfyJS.onChat=(user,message,flags,self,extra)=>{varnewMessage=document.createElement("li");newMessage.innerText=`${user}:${message}`;chat.append(newMessage);}ComfyJS.Init("YourTwitchName");
Enter fullscreen modeExit fullscreen mode

We're creating a new<li> element for each message we receive and adding the user and message to it. That gets appended to our<ul> block.

Sending message Now I'm in a an li element in chat and it appears in the DOM

We want to separate the user from the message and give it some spacing for our purposes. So we'll change the code to the following:

varchat=document.querySelector("#chat>ul");ComfyJS.onChat=(user,message,flags,self,extra)=>{varnewMessage=document.createElement("li");vartext=document.createElement("blockquote");newMessage.innerText=user;text.innerText=message;newMessage.append(text);chat.append(newMessage);}
Enter fullscreen modeExit fullscreen mode

Styling

Our chat is working, but we don't want to add it to our stream in this state😅. Let's add some CSS to make it look more impressive. Start by adding<style> tags to our<head>. To that, we're going to add these rules:

Fix the width and height of our chat

#chat{width:400px;height:350px;}
Enter fullscreen modeExit fullscreen mode

You can set this to any width and height. Just remember what values you've chosen for later.

Style the messages

#chatli{background-color:hsl(196,58%,93%);box-sizing:border-box;padding:1rem10px;margin-bottom:10px;}#chatul{list-style-type:none;list-style-position:outside;}
Enter fullscreen modeExit fullscreen mode

Note: We're usingbox-sizing: border-box; here so that our padding and margins don't increase the width of our messages.

Set chat to auto overflow

#chat{width:400px;height:350px;overflow-y:auto;}
Enter fullscreen modeExit fullscreen mode

We're setting theoverflow-y property because we want to affect the vertical overflow of the chat. We set it to auto because we want the chat to start its scrolling behaviour when it starts to overflow.

Display the chat from the bottom up

#chat{width:400px;height:350px;overflow-y:auto;display:flex;flex-direction:column-reverse;}
Enter fullscreen modeExit fullscreen mode

We want the last message to appear in our chat and push our older messages up and off the screen. To do this we set ourdisplay property toflex and set theflex-direction tocolumn-reverse.

Remove scrollbars

#chat::-webkit-scrollbar{display:none;}
Enter fullscreen modeExit fullscreen mode

At this point, our chat is starting to look decent. Below is the full stylesheet with additional rules to get our final result:

html,body{margin:0;font-family:monospace;color:hsl(197,62%,32%);}#chat{width:400px;height:350px;overflow-y:auto;display:flex;flex-direction:column-reverse;}#chat::-webkit-scrollbar{display:none;}#chatul{list-style-type:none;list-style-position:outside;}#chatli{background-color:hsl(196,58%,93%);box-sizing:border-box;padding:1rem10px;margin-bottom:10px;border:4pxsolid;}#chatblockquote{font-size:1.2rem;}
Enter fullscreen modeExit fullscreen mode

Sending message Very stylish on chat and it appears as a styled message in the browser

Adding animation

To make our chat look even cooler, we're going to have each new message enter and slide in from the left. First, we want to define our@keyframes animation:

@keyframesslide-in-left{from{transform:translateX(400px);opacity:0;}to{transform:translateX(0);opacity:1;}}
Enter fullscreen modeExit fullscreen mode

Next, we'll target our most recent message withlast-of-type and apply the animation to it:

#chatli:last-of-type{animation-name:slide-in-left;animation-duration:0.15s;animation-timing-function:ease-in;}
Enter fullscreen modeExit fullscreen mode

These 2 CSS rules are enough to give us an entrance animation. You can try and make the top message animate off screen usingli:nth-child() as a bonus activity.

Finally, to make the newest message stand out, we'll make our older messages a bit transparent:

#chatli:not(:last-of-type){opacity:0.5;}
Enter fullscreen modeExit fullscreen mode

Sending message Animation on chat and the message animates in on the browser
Here is the final result with the animation.

Conclusion

We've made a great looking chat overlay, and now you can show your audience what you've made on your streams. Here is atutorial on how you can add your HTML file to OBS. It will run just like it did in our browser, but remember to set the width and the height to the same values we used for#chat in our CSS. And here is a repo of this project onGithub.

This was just a taste of what you can do with ComfyJS. It's a great library for making fun projects for your stream. Check outPixelPlush,TrostCodes, andwhitep4nth3r for inspiration on overlay and interaction ideas. Thank you toLunchDevCommunity for their rudimentary chat overlay.

Lastly, I challenge you to extend this chat by setting different colours for your mods and subscribers (hint: you can use theflagsmod andsubscriber).


Thank you for reading my article. I'm new to writing, so any feedback on whether this was helpful or not, or topics you might be interested in are welcome.

See you at the next one!

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vastikrisz profile image
Krisztián Vastag
Just an IT student
  • Joined

Amazing article, it really helped out a lot. I'm really new in the frontend business (only a student) so I don't really have a deep understanding of JS yet. Could I perhaps ask you to tell me how I could display badges and emotes as well?

CollapseExpand
 
annetawamono profile image
Anneta Wamono
Hi there 👋, I'm a web developer at Studio Locale and an aspiring technical writer. I'm also a 2D animator and story artist for my own little films.
  • Location
    Kitchener, Canada
  • Education
    University of Pretoria, Conestoga College
  • Pronouns
    She / Her
  • Work
    Web developer at Studio Locale
  • Joined

Hi Krisztián,

Thank you for your feedback! Right now, with ComfyJS (which is the library used to connect with the Twitch chat) you aren't able to get the actual images for badges and emotes. There is a anextra parameter that contains information like the emoteid, but I haven't tried using it to reference an emote image. If someone else knows more, hopefully they can let us know.

CollapseExpand
 
casinoira profile image
Casinoira
  • Joined
• Edited on• Edited

I just tried developing a twitch chat overlay and figured out how to parse the emote and badge through its id.

With emote it is easier as you can just use this function I found from a tmi development thread here

For badge, get this JSON file. There may be a cleaner simpler solution, but I just loop through theextra.userBadges parameter and compare and grab the image from the JSON file above. A rather brute force way.

I hope I explain it well enough, I'm not really good with the technical web development term haha

Thread Thread
 
annetawamono profile image
Anneta Wamono
Hi there 👋, I'm a web developer at Studio Locale and an aspiring technical writer. I'm also a 2D animator and story artist for my own little films.
  • Location
    Kitchener, Canada
  • Education
    University of Pretoria, Conestoga College
  • Pronouns
    She / Her
  • Work
    Web developer at Studio Locale
  • Joined

These resources are great! Thanks for sharing.

Thread Thread
 
casinoira profile image
Casinoira
  • Joined

No problem, it's quite difficult to come by documentation or solution for comfy.js. Is only by chance I found out that it used tmi library for twitch that I was able to find out how to parse emotes and badges as image. Hopefully this is another tip for some other features that future developers want to develop, try to look into tmi development too.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi there 👋, I'm a web developer at Studio Locale and an aspiring technical writer. I'm also a 2D animator and story artist for my own little films.
  • Location
    Kitchener, Canada
  • Education
    University of Pretoria, Conestoga College
  • Pronouns
    She / Her
  • Work
    Web developer at Studio Locale
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp