Unity multiplayer in WebGL

Hi!
Sorry for the thread, I’m sure it’s something basic, but I can’t understand the topic well. I have found some information and they say different things, it is not clear to me.

I’m making a fight game, only two players. It has to be in WebGL. One player could invite another to play a match through a link. The idea is that the player who invites is the host of the match and acts as a server.

For example, the documentation of netcode says:

So, I would appreciate some information on the subject, what could be my options to approach the problem, a point where to start, frameworks that can host in WebGL, etc.

Thanks in advance!

Netcode cant support WebGL because WebGL uses WebSockets.

Transports are normally some kind of “wrapper” for something that can be used to connect over the internet (like the socket class or Steam Relay), so the networking solution can use them.
The advantage of doing it like this is that you can make all transports you want , and the code outside the transport doesnt have to change.

I guess netcode will use the Socket Class by default, which doesnt work on WebGL, so you will have to make a WebSockets transport or download one.

As for the hosting, the server itself cant be a WebGL build, so you will need a Azure, AWS or Google Cloud server, and when you want to host a game the player will send a rpc saying “hey, I want to host a lobby with this name”, and the server will handle it. Doing it that way has a lot of advantages:

-No port forwarding.
-Doesnt need the hosts computer ip.
-The server is full authoritative (You can give some authority to the host to do kick players), so the host cant cheat.
-Since the real game is not running on any client computer, but in a powerfull server, the preformance wont be a problem.
-Clients dont depeend on the host, and no host migration is needed, since the real host is a server on the cloud.
-Clients dont depend on how good is the internet of the host.

But all of this has a problem:
-Costs money.

Not sure which library ‘Netcode’ is, I assume mlapi?
As far as I remember, they use Transports as well.
And there are a few WebGL transports available, e.g. this one from james:
https://github.com/James-Frowen/SimpleWebTransport

You could probably convert this to any library which uses the Transport concept rather easily.
It’s usually just Connect/Disconnect/Send/etc.

Thanks for your answers!

Now, I’m using netcode for gameobjects (About Netcode for GameObjects | Unity Multiplayer Networking), with a transport that supports WebGL (multiplayer-community-contributions/Transports/com.community.netcode.transport.websocket at main · Unity-Technologies/multiplayer-community-contributions · GitHub).

I’m trying to connect a WebGL build as a client to a windows build as a server, I can’t figure out how to do it.
I have no problem doing the tutorials of the netcode for gameobjects. However, when i change to the WebSocket transport and a build webgl client, I can’t replicate the tutorials.

Any help would be really appreciated!

@maurod_unity any luck? I’m attempting the same thing. I can build native with that websockets transport but not webgl

WebGL is a graphics library, the websockets transport is what you want.

sorry, I wasn’t very clear. The NetworkManager (netcode for gameobjects) with a websocket transport doesn’t work in a webGL client build, even though the same setup works in a windows build

Are you using your WebGL build client as a Host or Server? If so, don’t, try using the Windows or Mobile builds as your server or host.

I’m only using the webgl build as a client.

@thamrick No luck :frowning:
Same problem, couldn’t solve it. So, I moved to Mirror (https://mirror-networking.com/) and it worked!
I could even setup a dedicated server (windows) and connect WebGL clients to it!

Awesome! Thank you for the recommendation! That’s looks like it’s just what I needed!

Hello Mauro!

This is exactly what I’m trying to do with Mirror. Do you have any tutorials to redirect me in order to do this? It’s my first time tackling networking with Unity!

For the easiest way to add game servers, check out our video tutorial using Mirror’s Tank sample:
https://www.youtube.com/watch?v=nEF1BPZAGZA

1 Like