Mazyod

Mazyod

Phoenix Channels C# Client

Hi all!

I’ve just shipped a C# Phoenix library, and wanted to share it here with some learnings..
https://github.com/Mazyod/PhoenixSharp

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:

  1. Loose dependencies. (Inject your own Websocket lib)
  2. Statically-Typed, Maintainable, and easy to use. (It has a lot more types than JS lib)
  3. Unity compatible. (After all, I need to ship my phoenix-powered game!)
  4. 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 and OutBoundEvents 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 the Push 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 :stuck_out_tongue:

Most Liked

ibgib

ibgib

I did C# since .Net 1, so I scanned through it and I think the C# code looks great! :smile:

Class and Method names are capitalized, local variables and private fields are pascalCased…the only change I would make is having the public properties/fields be PascalCased, thus differentiating between local variables/args and properties/fields. Also it is less common in C# to have the hanging indent-style curly braces, but I’ve seen some big projects that use that style like Caliburn if memory serves (framework by Rob Eisenberg, the dude behind Aurelia/Durandal).

So overall everything looks very consistent and clean from what I can see, so again, very impressive for not being a C# guy! :thumbsup:

Mazyod

Mazyod

Thanks for the detailed feedback!

What do you think about _privateField convention, is that the norm as well? Do private functions need a differentiation as well?

ibgib

ibgib

I personally used _PrivateField because it worked with snippets. Nowadays maybe you can dynamically change a snippet’s variable first letter casing. So I would have for property notified change properties, e.g.:

private string _MyProp;
public string MyProp
{
  get { return _MyProp; }
  set 
  { 
    if (value != _MyProp) 
    {
      _MyProp = value;
      NotifyPropertyChanged(value);
    } 
  }
}

Disclaimer: That’s from memory anyway (I’ve been doing only JS/TS/Elixir for the past year or two) and I’m on my phone! :wink:

I think I would use four spaces but on my phone I’m doing two…yes, here is an example of a more fully fleshed base class property. :smile:

Last Post!

AlejandroHuerta

AlejandroHuerta

Yeah I understand. I’ve run into some bugs with 4.6 profile so far.

SynchronizationContext is already set up for us so as soon as you call await it’s implicitly captured by Unity’s context. As for the return value the way I handled it is by essentially returning your Reply class and handling the status myself. Not sure there’s a better way to design that.

I’ll see if I can write up a PR this month.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New

We're in Beta

About us Mission Statement