# Theory on how to make a multiplayer game

**URL:** <https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327>\
**Category:** Demos and projects\
**Created:** [May 18, 2019, 11:12pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327 "2019-05-18T23:12:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 18, 2019, 11:12pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/1 "2019-05-18T23:12:21Z")

</div>

This is an idea I just thought of (like just today).

If you have a babylon.js project running, then a website, you could make a multiplayer game.

How it could work:

You have the game running with no cameras, but a function that can make one. When a user connects to the site, a `window.alert();` could pop up asking for a username. The input would name a canvas “username-canvas”.

Somehow (I thought this up today, haven’t thought this through) the function that can make cameras is triggered, adding a camera, connected to “username-canvas”, and control would be given to “username-canvas”.

That’s the basic idea. Not sure ho keydown, keyup, mousedown, mouseup events would work.

I wonder if something like this would work with sites like github, gitlab, or bitbucket.

Feedback would be nice. Just an idea.😉

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 19, 2019, 12:24am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/2 "2019-05-19T00:24:53Z")

</div>

Ive been using PubNub free tier to do my websocket handshake to connect users via webRTC, which I plan to use for p2p communication of player/world state, so as to avoid a centralized server and associated overhead.

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 19, 2019, 12:27am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/3 "2019-05-19T00:27:11Z")

</div>

If anyone has a readymade solution for webrtc game state transfer id love to see it.  
Seems as close as it gets to UDP

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 19, 2019, 1:41am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/4 "2019-05-19T01:41:49Z")

</div>

I also thought of another idea.

Running the game in the server can’t be done with a static hosting site. But I think I have a solution.

The first person to go to the site will be the “host”. They will run the game and others will connect to the host. If the host leaves than another person will run the game. Limit of 4 people maybe?

Because it is static everyone will connect to the same site, and run the game at the same time. Only 1 person will have everyone else connected to them.

Just a thought.

@withADoveInOneHand I’m making a multiplayer game and any pre-made webrtc solution would be great for me too.

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 19, 2019, 2:48am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/5 "2019-05-19T02:48:06Z")

</div>

Right, I think you want every client running the game logic, and everyone shares a world state.

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 19, 2019, 4:39am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/6 "2019-05-19T04:39:28Z")

</div>

It’s not webRTC but why not use something like [Colyseus Multiplayer Game Server for Node.js](https://colyseus.io/)?

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 19, 2019, 2:12pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/7 "2019-05-19T14:12:17Z")

</div>

I think I would be able to use Colyseus with my original idea.

Have the server run the game, and when someone connects, it makes a new camera and adds control. I gotta try that!

Thanks for the suggestion.

---

<div class="post-metadata">

**Author:** ![sharp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sharp/32/1325_2.png) [@sharp](https://forum.babylonjs.com/u/sharp)\
**Post date:** [May 19, 2019, 7:40pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/8 "2019-05-19T19:40:10Z")

</div>

I would use [https://socket.io/](https://socket.io/) but I didn’t know Colyseus and it looks like a great candidate !

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 20, 2019, 2:00am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/9 "2019-05-20T02:00:17Z")

</div>

Thanks for the suggestion! I should really make a list. I’ll have to try out so many game server options!

---

<div class="post-metadata">

**Author:** ![vickylance](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vickylance/32/2401_2.png) [@vickylance](https://forum.babylonjs.com/u/vickylance)\
**Post date:** [May 20, 2019, 3:25am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/10 "2019-05-20T03:25:15Z")

</div>

@Givo You can also try Nengi.js which was built for 3D browser based games. The author is pretty active and he has a discord channel where you would get pretty good help. He also has a demo using Babylon js and the library has features like Latency compensation. You can check out his videos on youtube as well for things it can do.

He is the author behind Nengi js

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 20, 2019, 11:16am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/11 "2019-05-20T11:16:16Z")

</div>

Wow! I gotta try this one first! The latency is low, and this guy also made [bruh.io](http://bruh.io), a game I’ve played with my friends before. Thanks for the suggestion! This one is at the top of the list!

Current list:

1. Nengi.js
2. Colysues
3. [socket.io](http://socket.io)

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 20, 2019, 7:47pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/12 "2019-05-20T19:47:39Z")

</div>

Yeah i guess running my own xmpp/socket server would be best. My AWS guy is always swamped though ×p I bug him too much.  
Running an unordered p2p webrtc data stream will still need an ice/stun/turn server though… Which who knows how to set up.

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 20, 2019, 8:07pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/13 "2019-05-20T20:07:09Z")

</div>

AWS?

I’m to cheap for that. That’s why I use gitlab and heroku.

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 20, 2019, 8:44pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/14 "2019-05-20T20:44:13Z")

</div>

Right, and it’s no walk in the park. Never tried gitlab (git io is slick though), heroku pay tier was… not a great bargain.

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 20, 2019, 8:54pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/15 "2019-05-20T20:54:29Z")

</div>

Gitlab is like github, but it has an amazing Web IDE. Have you tried heroku free tier?

Gitlab’s Web IDE:

 ![06%20PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/6/6f63cdf26f204fed9f06723b300c518e426831b7.png)

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 20, 2019, 9:56pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/16 "2019-05-20T21:56:43Z")

</div>

Oh yeah that looks real straight forward. Free tier did treat me well, i just wouldnt marry my architecture to it (or any pay service for that matter, guess thats common sense)

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 20, 2019, 11:00pm UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/17 "2019-05-20T23:00:21Z")

</div>

Oh. I might have to host my own sever then for my game.

Yeah, Gitlab is pretty simple. Did I forget to mention that you can open many files at once?

 ![58%20PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/b/b28fd1126260ba2c4a94d63c8b93a160da85ae67.png)

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 21, 2019, 12:46am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/18 "2019-05-21T00:46:56Z")

</div>

Oh yeah a web ide is something i gave up looking for, will have to take it for a spin.

---

<div class="post-metadata">

**Author:** ![Givo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/givo/32/4968_2.png) [@Givo](https://forum.babylonjs.com/u/Givo)\
**Post date:** [May 21, 2019, 1:14am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/19 "2019-05-21T01:14:32Z")

</div>

Definitely recommended, 8.5/10! (1.5 taken off for the web IDE not being the default editing thing in gitlab)

on a repository you have to click the web ide button!

 ![Screenshot%202019-05-20%20at%2081](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/6/6542d978e31cf4d7f3ec689a8354cfbac7b691a3.png)

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [May 21, 2019, 1:24am UTC](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327/20 "2019-05-21T01:24:27Z")

</div>

❤ The 411 👊

[Next page](https://forum.babylonjs.com/t/theory-on-how-to-make-a-multiplayer-game/3327.md?page=2)
