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:

Where Next?

Popular in Discussions Top

mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

hakarabakara1
I have three modules, a Business which is associated Tags and Categories though join tables business_tags and business_categories. I inte...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement