Devlog: Making watchcord

For the past few months, I've been working on and off on watchcord, an app that allows you to use Discord from your Apple Watch.

Devlog: Making watchcord
Photo by Luke Chesser / Unsplash

For the past few months, I've been working on and off on a project I call watchcord, an app that allows you to use Discord from your Apple Watch.

For this project, I learned how to use the Discord API to load servers, channels, and send/receive messages.
I was also going to use Discord's Gateway API, which uses websockets to communicate with clients in real-time, but unfortunately I'm not able to, as Apple heavily restricts usage of "low-level networking", detailed in this article by Apple.

An excerpt from the beginning of the article:

watchOS groups networking into two categories:
High-level networking.  This includes the HTTP and HTTPS support in URLSession, and any code layered on top of that.
Low-level networking.  This includes Network framework, NSStream, and any other API that runs a TCP connection or UDP session directly.  That includes the low-level aspects of URLSession, namely URLSessionStreamTask and URLSessionWebSocketTask.   It also includes APIs, like NWBrowser and NSNetService, that interact directly with Bonjour.

So, instead of using websockets for real-time communication (i.e. receiving messages in the currently open channel and updating as they are processed), I need to update the app soon to request new messages every few seconds using HTTP GET requests.
I've also linked my Gateway API class code at the bottom of this article.

My current implementation of receiving messages via Discord's API works by sending a GET request to https://discord.com/api/v9/channels/CHANNEL_ID/messages (with proper authorization header), and converting the response to an array of my own custom Message class

Conversion of the GET response to an array of custom Message classes

Currently, watchcord can only view servers, not direct messages, but I'll implement being able to message friends or group chats next update.

It wouldn't be that hard to add DM support to watchcord either as all I would need to do is just modify the class that fetches all guilds to fetch from https://discord.com/api/v9/users/@me/channels instead of https://discord.com/api/v9/users/@me/guilds

Eventually, I'm also going to add support for voice chat using CallKit and websockets (which I CAN use because you're allowed to use websockets with CallKit)

There is also a testflight for in-dev builds of watchcord, and if you'd like to get access to it, contact me on Discord.
https://circulars.dev

Anyways, here's my "scrapped" implementation of Discord's Gateway API in Swift: