Elixir from NodeJS - is it right for me?

Hey everyone!

I am a nodejs dev, and have a gameserver (utilizing Websockets) that is currently sitting around 20k lines of code. Our game is developed in JavaScript and it’s basically my main language. I love it. The problem is as you know, since Nodejs is single threaded, we are having to spin up multiple instances of node. And then ontop of that, since we cannot share memory between processes, we also need to use Redis. Not saying that’s a bad thing, and it works fine, but I just feel like we’re not taking advantage of a server’s full potential (it’s hard to explain).

However, I’ve gotten to the point now, since our game is instanced based and I want players to be on the same instance as a game that they created… Nodejs is really becoming a pain in the ass. I heard elixir handles all the “single threaded issues” that nodejs has for you and utilizes your entire server regardless of how many cores you have. Is that true? And do you think moving over elixir would be beneficial? I know I would have to do some code conversion and it will take a while… I am also curious if elixir would be fine running under debian as that is our main server OS.

I am already reading the docs now and the syntax is a bit different from JavaScript (and I mean really different) – but, it’s not that bad and I am actually starting to like it. Thanks for reading and looking forward to the responses!

12 Likes

Yes, because in Elixir data is immutable, and no state is shared between processes, it is possible to have true concurrency without deadlocks.

Other things Elixir will do for you:

  • You’ll probably not need Redis, as there are many solutions for data storage (both persistent and ephemeral) that work inside your Elixir application.
  • You’ll only need a single Elixir server (opposed to multiple node servers), as it will be able to evenly distribute itself over all CPU cores. But if at some point this turns out to be not enough, it is possible to start a second server whose processes communicate with the first. – this is built into the language.

JavaScript has grown through the years, but it still is mostly an imperative programming language. It will take some time to adjust to writing and using Elixir, but I think it will be worth it. (It is always good to learn different paradigms, if only to make you stronger in the language you predominantly use)

There is a very nice web-framework you can use to enhance your Elixir application called Phoenix, which is really good at handling websocket connections.

To summarize: Yes, Elixir is probably right for your use-case. :slight_smile:

10 Likes

The main difference between Elixir/Erlang app, and something like Node.js or Ruby app, would be what you already described as your problem. Generally you spawn X Node.js processes, or Y Ruby processes. They have limited abilities to communicate. In Ruby you can use threads but they are difficult, and are error-prone and can degrade performance. In JavaScript, well, there’s only one thing happening at a time.

Elixir and Erlang apps work in a different way. Instead of spawning multiple system level processes, your application would consist of multiple Erlang processes. Those processes are basically looped functions that wait for incoming messages or do some stuff on regular intervals.

These processes communicate by sending messages to each other, rather than calling functions. This is important bit, because it allows you to build Elixir cluster. When you start your Elixir app, it runs in a cluster already, that has 1 node in it. But you can add more nodes to the equation, and processes will be able to communicate across network, as they were on the same host.

Okay. To sum up: I think Elixir would be perfect for your problem. This is already proven by many users using it in similar context to yours.

10 Likes

Is there any way you could work a small spike in Elixir? Something that would allow you to try out a little bit of Elixir for yourself without committing to totally converting your codebase?

No doubt I’m a huge fan of Elixir so I’m really inclined to say “Elixir will beat Node for multithreading” but that’s not the only concern you should have. One thing:

Elixir won’t allow you to share memory between processes either. This is actually a good thing in terms of scaling and reliability because if one process crashes it won’t corrupt memory which may be used by other processes. And, to be sure I’m being clear–Elixir processes are not the same as OS processes.

Overall though my suggestion would be try a small project with Elixir and see how it works out for you. Then if Elixir seems like a fit start moving your existing stuff off of Node piecewise as you can.

6 Likes

Maybe can I am not 100% elixir expert, but I can some feedback.

Elixir runs on Erlang virtual machine. (good support linux/windows -very mature) It has concurrent actor based model ( The actor model in 10 minutes ). What this mean it is concurrent and fault tolerant. Maybe I will not get in the details but Elixir/Erlang is best suited for creating distributed fault tolerant systems Let It Crash! The Erlang Approach to Building Reliable Services with 99.99% availability. Due to actor model it easy scale to use all CPU resources.

I think it will be good suited for your needs but except language itself you will need to learn Erlang OPT -> how to design concurrent applications in Erlang/Elixir based on actor model.

PS
If you still want to write code in javscript and have something more than nodejs can offer check http://vertx.io/

5 Likes

@Qqwy

You’ll only need a single Elixir server (opposed to multiple node servers), as it will be able to evenly distribute itself over all CPU cores.

Wow, well that just got me onto the “Elixir Train”, if you will. I feel like I just wasted a ton of time with node now… hearing this. But I also feel excited that something like that does exist, I’m very eager to jump in the docs / books and start learning.

Thanks for your response Q, it’s really appreciated. The Phoenix framework just makes it seem like the language gets better and better! I will be looking at Websocket benchmarks later as I have a feeling it blows node out of the water.


@hubertlepicki I will be bookmarking this thread because It will take a bit of time for all this information to sink in. I feel happy that I was not the only one :).


@Onor.io Yeah, good idea. I was thinking of just setting up the Phoenix/Websocket framework first and then once I really feel comfortable I will start with converting our RPG functions. I found this, and It looks like a good tutorial to start with. (I hope :P)


@mkunikow Hey, thanks for those links I will check them out! After hearing about Elixir though, I don’t even want to go back to node anymore… Or anything else. I just wish I started on Elixir first before wasting my time, but since our game is actually written in JS (clientside too), I guess it’s not really that much of a waste. I guess I have “JavaScript” under my belt. What an accomplishment! /s :slight_smile:

Anyways, thank you for the responses. Didn’t know I was going to get that many, I’m diving into the docs and tutorials now.

10 Likes

Apart from the technical advantages as covered by everyone else, you also get a much, much nicer syntax which has been heavily influenced by Ruby (designed for programmer happiness).

Another consideration is learning material - we are very lucky to have some awesome Books which will help get you up to speed quickly.

Good luck and keep us posted on how you get on :023:

8 Likes

It sounds like you’re taking a good path to evaluate the situation. Good luck! Even if Elixir turns out to be something other than what you need, I’m sure you’ll find the experience enriching.

5 Likes

@dillybob and we have 25% discount on PragProg ebooks :wink:

4 Likes

“Elixir from Node JS: The Movie” - kind of. :slight_smile:

What every Node js developer needs to know about Elixir - Bryan Hunter

6 Likes

Aside from all the amazing things Elixir (Erlang/OTP) provides think how much your life would have being easier with Node if you could write blocking code and have long running tasks without worrying about blocking the event loop, which you totally can in Elixir. All your experience with async code is still handy since the only way processes communicate is by asynchronous message passing.

4 Likes

After going back to spending a lot of time with node can’t reiterate this enough :slight_smile:
Although have to admit things are progressing in Node land async/await and Yarn make some old pains go away.

3 Likes

Hi dillybob. How is the elixir gameserver going? I’m curious to know if you had any issues. Any gotchas? Correct me if I’m wrong but one thing that might be a disadvantage with elixir is that it’s not possible (AFAIK) to run the same js library such as p2 physics on the server and client. Maybe those libraries can be accessed from elixir with ports but that would add complexity. Another possibility is to use a c/c++ library with webasembly and access the library with port on the server and webassembly on the browser.

Is it still worth using elixir even with the added complexity of having to use ports or NIF etc for game servers?

Are elixir ports low latency enough for game servers?
I haven’t been able to find instructions on how to integrate node and elixir.

1 Like

Hello,
It’s been more than year now. Just curious to know what decision did you make. Did you use Elixir ? If yes , was it better than Node Server. I am also moving from node to elixir.

1 Like