I want to experiment with a swift peer-to-peer gaming network that i built myself, but where to begin?

Hello there. As the title implies, I want to build an online peer-to-peer gaming network using swift so that I can conduct a few experiments that I have in mind. However I’m new to networking and I’m having a hard time finding the right resources to get started. Any suggestions?

Again i want to build this network from scratch, therefore any code that can be used as a reference as well as any existing foundations (URLSession good for gaming??) would be appreciated.

Thank you for your time.

There's https://developer.apple.com/documentation/multipeerconnectivity that works as a baseline, but the API is somewhat awkward to use and can be a bit slow to establish into a full connection. It leverages both Wifi and Bluetooth locally, and once established the connection is pretty decent. I wouldn't be surprised to see this either deprecate or evolve significantly in the next year or two as Actors in swift, and more specifically Distributed Actors, get established and into the base language, and more systems can be built atop them in a pretty reasonable format.

In the past there was a GameKit mechanism that supported Peer to Peer networking as well (https://developer.apple.com/documentation/gamekit/gksession) - although it's now deprecated, so it's more for awareness than anything else. GameKit itself appears to have shifted a bit more to internet-friends connectivity, rather than a strict peer to peer model, but it may still be worth investigating depending on your needs: https://developer.apple.com/documentation/gamekit/connecting_players_with_their_friends_in_your_game

Beyond that, there's WebSocket support in URLSession these days, and StarScream if that's falling short - but you'd need to host your own HTTP services on some device and come up with an advertising process to let other devices know about it for peer to peer. If you want to go the route of hosting an HTTP service within an iOS app, it's possible with SwiftNIO - has been for a couple years now, with a decent article talking about it at https://diamantidis.github.io/2019/10/27/swift-nio-server-in-an-ios-app - and perhaps most interestingly, the article references a couple other libraries that let you do the same. Hopefully some research down that path provides interesting food for thought.

It leverages both Wifi and Bluetooth locally

FYI, Apple’s peer-to-peer networking [1] hasn’t used Bluetooth in a while (iOS 9 maybe?).

Beyond that, there's WebSocket support in URLSession these days

URLSession has WebSocket client support, but for peer-to-peer you need both client and server. For that you need Network framework. Honestly, it’s a better option anyway (-:

Network framework supports peer-to-peer networking, WebSocket client and server, and PSK (shared secret) TLS. That’s my go-to combination for this sort of thing. Check out Building a Custom Peer-to-Peer Protocol.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] “Peer-to-peer” has a bunch of different meanings depend on the context. In this context I’m using it to refer to infrastructure-less networking, that is, communicating between two nearby peers that don’t share any common infrastructure, for example, are not on the same infrastructure Wi-Fi network.

I want to experiment with a swift peer-to-peer gaming network that i built myself, but where to begin?
 
 
Q