Real-time systems with Combine and WebSockets
I always felt like WebSockets were the final boss of software engineering. Back when I was very junior, I made an app with my friend. Me on the front-end, him on the server. But I couldn’t understand how to get his APIs to work, so he built my networking code for me. Neither of us really had a clue what we were doing—he’d created a WebSocket server to deliver static JSON. His networking code was littered with Ancien Régime CoreFoundation invocations such as CFReadStreamRef and CFWriteStreamRef. Back in the day, this was the only way you could do WebSockets. Today, Apple offers us much cuddlier APIs. I was a mid level developer with a chip on my shoulder when Combine came out in 2019. This was the perfect time for me to enthusiastically apply FP-style operator chains over every API surface I could. If you asked me, I’d insist it was neater. Maybe I even believed it. But we all know the truth: I had to make it more complicated to show I was smarter than everyone. Nevertheless, Combine (when used appropriately) is one of the best ways to design reactive systems. When used alongside WebSockets, they produce a beautiful harmony. Real-time networking on the backend; reactive UX on the frontend. Today we will look at my sample project containing three realistic use cases for real-time systems. I set up a local WebSocket server that and 3 simple SwiftUI features. Everything on the iOS side from networking to UI will be powered by Combine.
Continue reading this post for free in the Substack app |