Mazyod

Mazyod

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:

Showing Posts 1 to 10

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 OP

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:

Kabie

Kabie

Cool!

I’m using https://github.com/jfis/dn-phoenix with some tweaks. And I do found there are some JS stuff does not fit into C# world. Glad to see a client not translated line by line from JS. Here are some of my changes:

  • The Timer does not play well within UnityEditor, so I removed the auto reconnect code, let the game decide what to do
  • Added Push.Receive<T>(string, Action<T>)
  • Also added shortcuts Push.Ok , Push.Timeout , Push.Error

Best HTTP looks useful, will also check it.

Mazyod

Mazyod OP

Those are some excellent notes!
I’ve had the same issues you described, the timer issue was especially
annoying.

In my library, I make sure to clean all timers when you call disconnect.
So, even in my game, I always call disconnect and use my own coroutine
based retry logic.

The generic variant of receive looks very useful. I think it will be a good
addition.

Feel free to open issues or submit PRs when you find the time :smiley:

AlejandroHuerta

AlejandroHuerta

Hate to resurrect this thread but is this still something you are actively working on?

Mazyod

Mazyod OP

@AlejandroHuerta Yup! My game is still in production, and the library is still actively maintained.

AlejandroHuerta

AlejandroHuerta

Ah that’s great to hear. So you have any plans for the c# upgrade in beta? Such as async/await?

Mazyod

Mazyod OP

Not yet, that will have to wait until unity officially rolls out support for those features.

AlejandroHuerta

AlejandroHuerta

Wont be waiting very long as 2017.1 is in beta and scheduled to release in July with support for it. Anyways I’m glad you’re still supporting your library. Seems to be the only one still being actively worked on.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews