Writing Code for my Twitch Stream

Dwayne Harris   ·   About 917 words

I started streaming on Twitch recently!

I tried it out like a year ago to see if I would be into it, but I didn’t really have the equipment at the time so I didn’t stick with it. But a couple of months ago, I upgraded my desk/workstation situation and finally decided to buy a webcam for my gaming PC. Which, by the way, I had to get on eBay since webcams have been completely sold out since we’ve all been staying home and so I had to pay 50% more than I would have otherwise.

C922x Pro Stream Webcam box
The webcam.

With those things set up, I finally opened up OBS after months of it sitting there on my taskbar doing nothing. It was pretty easy for me to set up a decent scene collection and put together a stream that looked aight. The only problem I’m still dealing with is getting my mic to sound loud enough.

There were a couple of things I ended up writing code on this website for that I want to talk about.

1. Displaying my music on the screen

I like playing music from Spotify while I stream and as soon as someone asked me what I was playing I knew I wanted to show it on the screen and have a chat command for it. Neither of those are easy to do with OBS.

I’m calling out OBS specifically because there are alternatives (like Streamlabs OBS) that let you integrate with other services and use preset layouts for things like music and chat. OBS doesn’t really do anything like that so you have to do things a little more manually. I’m not switching from OBS though because I like it and I refuse to switch to something else just because it integrates with stuff for me.

Anyway, with OBS, the best way to show a custom display like what I wanted to do for music (basically just album art and track/artist/album info) is to use a web view that points to a website or local webpage that has what you want to show. I eventually decided to create a page on this website that will always show exactly what music I’m playing. It keeps the page updated by polling a json endpoint I setup which is useful for the chat bot I made that I’m gonna talk about later.

In order to have the website even be able to show that information, I had to write some more Spotify API code so I can authenticate properly and get the “currently playing” info. I got all that setup and the result is this widget. It will show you what I’m listening to if I’m listening to something, and if not, it’s just a blank page. That way it’ll disappear in OBS when I stop my music.

I have OBS pointing to that page, and now I have a dope Spotify widget in my stream.

Screenshot of my spotify widget page
This is what it looks like in the browser. It's just the widget itself when it's displayed in OBS.

2. Running a chat bot

A lot of Twitch streamers have bots running in their chat rooms that will respond to commands with things like:

  • The current song playing
  • Social links
  • Uptime (how long the stream has been running)

Luckily it’s pretty easy to do since the Twitch chat is pretty much just IRC. I wrote a bot in Go that uses go-twitch-irc and I’ll just run it on my laptop whenever I start a stream. I’m also thinking about running the bot itself on this server so it can stay on permanently.

Two of the commands make API calls to this website for info. One is !song (or !music) and the other is !weather.

Screenshot of my twitch chat bot messages
The !song and !weather commands. The bot posts under my name with [Bot] prepended to the message.

!song

This uses the API endpoint I mentioned earlier (/api/spotify) that was made to keep the spotify-widget page up to date. It just does a basic GET request and caches the result in memory.

!weather

This uses a new API endpoint (/api/weather) I made that uses the weather functionality I wrote a few weeks ago to display it at the top of the pages here. I’m using emoji for weather icons so I hope that works on all platforms where people are reading Twitch chats.

Thoughts

This all went better than expected. Writing the bot was cool. And I’m glad I decided to keep building off what I started with integrations into this website. It keeps being useful for things like this. I have more Twitch/website stuff planned so I might be writing more about it soon.

Feedback
  ·   6 Likes
Last Updated

Contribute

Was this post useful to you? Want to support this website? Learn more. Thanks for reading!

Latest Webmentions

None yet.

Comments

No comments yet.

Add Comment

Add a Code so you can edit or delete this comment later. Using the same Name and Code for multiple comments will link them together. Learn more.
By posting a comment, you are agreeing to the Terms of Use.