Discord Activity

Box of Cards inside a Discord call. One click and you're in a game, and your friends join the same table. Part of the Box of Cards work.

TS · React · NestJS · Discord Embedded App SDK · Socket.IO · OAuth2 · AWS CDK · Vite · MongoDB · Mongoose · AES-256-GCM · Jest · Vitest

Why a Discord Activity?

why-discord

Box of Cards is best when you hear your friends laugh. On the website someone first has to get everyone into a voice call and then share a link. On Discord the group is already sitting in the call. One of them clicks Activity → Box of Cards and is in a game, and the others join through Invite. The website already gets you into a game with one click, but here the barrier is even lower.

The free exposure helps too. How much is hard to say. I counted a few hundred games on Discord and only a small part of them is in my genre, so there isn't much competition.

One Launch, One Table

launch-flow

After launching the Activity the player is already in a running game. Behind the scenes the Activity opens /play and logs them in with their Discord account. Their avatar shows their Discord name, or their server nickname if they have one.

The website's login sends you to Discord and back. An Activity can't do that, because it runs in an iframe inside Discord. So the Activity asks the Discord SDK for a code and my login server, the broker, trades it for a session token. The broker also asks Discord whether the player is really in this call's Activity. It's the same account as the Discord login on the website, and no email is needed.

The first player in a call creates the game and everyone after joins it, even when two people press start at the same moment.

Logging In While the App Still Loads

launch-before-after

The first version waited for the whole app before showing anything, built the board twice and sent about ten requests one after another. Now the login runs while the app is still loading and the board is built once. Every launch is 1.4 s faster and nothing flickers while loading.

After the first login the device remembers the player, so a relaunch skips Discord's login and is about 3 s faster.

Most of the remaining load time is waiting on Discord. Its OAuth login alone takes 1.5 to 4 s, slower than I'd expect for something this essential. Maybe it also checks the account's other devices.

What the Activity Leaves Out

activity-build

Inside Discord every request goes through Discord's proxy, which blocks any server without a URL mapping. So the broker is mapped to /broker and the five game servers to /hall1 to /hall5.

Analytics and ads don't work there, and neither does the service worker. Instead of if-checks all over the website, one build flag makes a separate Activity build without them. The Discord SDK is only in that build, so the website doesn't load it.

The Activity only has the game itself. The deck editor and the account settings stay on the website.

Built for Discord

discord-features

The invite button now opens Discord's own invite menu instead of our join link, which would lead out of Discord, so I restyled it. Sharing goes through Discord's share menu too. Downloading images and videos isn't possible inside Discord, but sending images straight into a channel or to friends works really well.

Your Discord status, the rich presence, shows friends what's going on at the table: the deck, the round, the players and how long the game has been running.

Ten Times Faster Than the Play Store

playstore-vs-discord

I already knew the Discord Developer Portal from the Discord login on the website, and I like it a lot. Creating the Activity there was easy and the whole thing took a few days of work.

I only found one bug. The Activity's page in Discord's app directory uses the bot avatar as its header and upscales it 3x, so it looks very pixelated.

Compared to the Play Store release it went about ten times faster, with a thousand times less text to read. It feels like the developer is really the focus here.