Engine for Browser Multiplayer Game using WebGl

Hello everyone, im currently working on a multiplayer browser game, which i want to host on my linux server. After looking how to implement the multiplayer, i decided on the mirror API, but all the tutorials i could find show that the game will be hosted by a player himself. Is it possible with mirror to have the serverlogic run on my server after i export to WebGL? Otherwise what are some good free alternatives to develop a browser multiplayer game, which i can host on my server? I already decided against Photon bc of the 20 player limit and against UNet because its deprecated soon

I am working on similar setup for my current project.

Long story, short – yes, you can totally run a “headless” Unity process as a Mirror “HOST” on your Linux box. Once you don that, you can can build WebGL game “clients” which will access your Linux server / Mirror HOST.

For your host / server you would just go into to your Unity build settings and do the following:

  • set target platform to “Linux” (assuming you have that installed on your build machine)
  • select “server build” (this removes the GUI from trying to pop up on your server)
  • click the “Build” button
  • create a folder for your Linux build
  • pick a name for your build binary
  • After the build completes, move that entire folder over to your Linux box
  • set up the binary to execute with chmod 755 ./your_game_name for the binary
  • run the build via a command which looks something like ./your_game_name

Assuming that your game is setup like mine is and the game requires the HUD GUI to start the hosting process, then the next step is to write some code wrapped checks for #ifdef UNITY_SERVER or UNITY_CLIENT on your host’s first “scene” to recognize that it is a server build and to automagically setup and start listening for connections as only a HOST.

Full disclaimer, as of writing this, I haven’t done the final step in this process for my project yet, so I might update later if I learn that it doesn’t work as I expected. Also, I am not an expert on this process yet, but let me know if you have any other questions and I’ll try to help where I can.

(edit: replaced backtick code comments with ICODE blocks)

2 Likes

To run Mirror on WebGL you will also have to use a transport which uses web sockets.

3 Likes

Verified that the #ifdef UNITY_SERVER works like I expected it to – that code only fires if you have compiled with the “Server Build” checkbox (assuming you are in Unity 2020 – I think it was called Headless or something similar in previous versions).

So my new flow for my project will be:

  • create a new empty startup scene
  • place a new startup scene as scene 0 in the “scenes to build”
  • on a startup scene, have one game object with a single script which will check for UNITY_SERVER and fire up my Mirror HOST behaviors without waiting for user input
  • if this is not a UNITY_SERVER, then move onto the “main menu” scene (or whatever your game client would normally start with)

This way I can compile for webgl clients and the dedicated server just by switching the target platform and check the “server build” in build settings. If I discover any other optimizations for this process, I’ll send them your way.

2 Likes

I like to write small tools to create the builds. Makes it very easy to build the server for a different platform and run a different scene in each of them. Something like this:

#if UNITY_EDITOR
public static class BuildTool
{
    [MenuItem("Tools/Build/Build Client")]
    public static void BuildClient()
    {
        var bpo = new BuildPlayerOptions
        {
            scenes = new[]{"Assets/Scenes/MainClient.unity"},
            locationPathName = "Build/Client/",
            options = BuildOptions.None,
            target = BuildTarget.WebGL,
        };
        BuildPipeline.BuildPlayer(bpo);
    }

    [MenuItem("Tools/Build/Build Server")]
    public static void BuildServer()
    {
        var bpo = new BuildPlayerOptions
        {
            scenes = new[] { "Assets/Scenes/MainServer.unity" },
            locationPathName = "Build/Server/MyServer.x86_64",
            options = BuildOptions.EnableHeadlessMode,
            target = BuildTarget.StandaloneLinux64,
        };
        BuildPipeline.BuildPlayer(bpo);
    }
}
#endif

This creates a Tools menu item which you can use to create builds. You could also add an option to build both with just one click by calling both BuildServer and BuildClient. (BuildOptions.EnableHeadlessMode is the same thing as ticking the Server Build box in the build settings)

3 Likes

Mirror is now finally supports SSL for WebGL builds, highly recommending using it.

3 Likes

I've set up a dedicated Mirror server in an AWS EC2 instance and have it working with many WebGL clients connecting to it. All the players are using NetworkTransform so that their movement is synced. However, I am running into MAJOR issues with latency. There's a ton of lag when connecting even 2 players to the server. I'm trying to figure out if this is an issue related to the SimpleWebTransport websocket protocol that Mirror uses to work with WebGL. Is it just a very slow transport compared to Telepathy/KCP? The latency is so terrible I'm going to have to re-write all of my code to Photon PUN instead. I really wanted to use Mirror because I wanted a server-authoritative game, but the lag is non-playable.

First: Websockets are built on TCP, and thus are “reliable”. What this means is that all data will be delivered, in order, whether it is old (newer information is available) or not. That in turn can be terrible for latency sensitive games, and what’s worse is there’s no way around it (on WebGL). Now, how various networking solutions’ higher layers handle the fact they’re running on a reliable protocol differ to various extents. For instance, obviously the more data transmitted the greater chance of running into issues. However, even a very streamlined networking solution running on websockets cannot break out of the constraints TCP places.

Second: Not all types of AWS EC2 instances are equal - you should probably describe your setup. There are some pretty terrible cheap AWS EC2 instances completely unsuitable for games. Either because of poor network capacity or simply that CPU is optimized for short “bursty” tasks, rather than the long running tasks game servers constitute.

1 Like

Profiling a multiplayer game is super tricky – so I’d recommend you get it working really smooth in editor FIRST, and then switch over to dealing with the headaches of running it in WebGL.

Try using something like ParrelSync to setup and run two copies of your game in editor with the profiling window up. Connect both of your editor instances to your dedicated server and post some screenshots of their profilers – as well as screenshots from your ec2 instances “Monitoring” tab for all the metrics covering the same period as your testing.

Then we can help you pinpoint where the problem is. After you know where the problem is, then you can begin to work to resolve it.

@atomikm Any update on your project,
Also what is the max amount of players you got connected per client?

Yes, so the issue had to do with the SimpleWebTransport protocol that Mirror uses for WebGL. It is very slow when using a website that requires SSL.

Two solutions:

  1. Use the Playflow cloud server (recommended) - https://playflowcloud.com/
    You can upload your Mirror server builds super easy to this cloud solution. The developer behind this solution has setup a reverse proxy server and it handles the SSL signing for you. Very low latency with SimpleWebTransport. And it also handles server scaling/AWS EC2 management etc. Highly recommend - this is what I’m using. I’ve connected 8 players with low latency

  2. Use AsioTransport - I didn’t get too far into doing this so I’m not sure if it works fully. But it is an alternative to SimpleWebTransport and is much more performant. You can find the developer of it in the Mirror discord

1 Like

How can i create a simple 3d metaverse webiste in unity and host it on web using mirror

how to use mirror on webgl for multiplayer .

1 Like

There is nothing simple about creating an online scam with corporate mumbojumbo and technobabble buzzwords !

2 Likes

3D metaverse website?
You can build a world that will act as your “Website”.
The main domain will redirect to your metaverse game…

But it’s not really effective for main websites since Webgl is very slow at startup.
You better use threeJS which is a much better tool for 3D websites and also supports mobile.