Hi all!
I’ve just shipped a C# Phoenix library, and wanted to share it here with some learnings…
Why even bother building another C# client?
Well, current ones I’ve seen either don’t support Unity, no longer maintained, or tightly coupled with certain a websocket dependency.
What problems will this new client library solve?
Prioritized based on my needs:
- Loose dependencies. (Inject your own Websocket lib)
- Statically-Typed, Maintainable, and easy to use. (It has a lot more types than JS lib)
- Unity compatible. (After all, I need to ship my phoenix-powered game!)
- Unit + Integration tests
Structural Changes to the JS Client
The JS client was “too smart” for me. It has loosly-typed APIs, and the code is super compact, which took me a while to really understand. That might be good for JS and an experienced Phoenix dev, but not for the uninitiated.
(An example is how the JS client doesn’t draw a distinction between sending events internally or externally. Both data flows leverage the recvHooks
and bindings
fields)
Instead, I’ve taken the time to break down everything so when you start looking at the code, the code reads as a client spec. A good example of that is how the phoenix messages are implemented:
- The
Message
class encapsulating the parameters details “{event, ref, payload, topic}”. -
InBoundEvents
andOutBoundEvents
enums, to distinguish[close, error, reply]
from[join, leave]
, respectively. - There is also the
Reply
struct which represents the{status, payload}
reply, so we can properly map it to thePush
object.
Then, with all this static typing, the library greatly simplifies the mapping of messages, since it now knows about control messages which should be kept internal in the library, and external messages, which should be dispatched to the listeners.
What’s Next?
There is so much work to do! I haven’t had time to implement Presence
, especially since I don’t use it in my game, but PRs welcome! Also, the library is currently pre-v1.0, since I’d love to hear suggestions on how to improve the APIs even further, and open to contributions.
… Last but not least … I do apologize for the coding style. I’m a Swift guy by trade, so I don’t know what C# people usually do