Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can WebGL project be the host in webserver (vps)

Discussion in 'WebGL' started by JoaoFlora, Jun 27, 2023.

  1. JoaoFlora

    JoaoFlora

    Joined:
    Dec 1, 2022
    Posts:
    3
    Hi everyone and if this is a noob question, i'm getting started with unity so sorry in advance.

    I'm trying to make a webgl multiplayer project that it is uploaded (hosted) into a vps (ubuntu), and it can be executed from a browser.

    And the goal of my project is to have the client and server running only in webgl (it could be different projects, but both must be running on webgl), but I'm having dificulties in understanding how i can make the server of my project running on webgl, because based of what i saw in the internet, the server could only be executed in unity editor or windows/linux application (not in webgl), but since i'm using a vps based on ubuntu i'm hoping that is a chance to make server work on webgl.


    I searched in google for answers and for example this package of webrtc isnt compatible with webgl:

    https://docs.unity3d.com/Packages/com.unity.webrtc@2.4/manual/index.html


    Mirror isnt a solution either because it isnt compatible with webgl because of kcp transport (not sure if it is the only reason):

    https://mirror-networking.gitbook.io/docs/manual/transports/kcp-transport


    I also found the page below, but i'm not sure if it is the answer to my problem (basically i dont know if websockets are the only problem with this server/webgl problem):

    https://docs.unity3d.com/Manual/webgl-networking.html#UsingWebSockets


    Many thanks
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,028
    This is not possible. If a web app - any web app - would be able to enable other remote participants (users or apps) to join their "service" in any way, then it would be a MASSIVE security issue for the entire worldwide web.

    To that end, Web Browsers execute WebGL apps in a sandbox. And within that sandbox an app isn't allowed to receive incoming connections. Therefore no other external services can "join" a web app.

    At least that's my understanding.

    Anyway, you don't need the WebGL app to also host. At least I could think of no reason why running the server on the same platform than the clients would be of any benefit. You wouldn't run a server on a mobile phone either.

    Since you have to run the server on a machine that is remotely accessible, you make the server app an app for that machine's OS, most likely Linux but could also be Windows but you do pay more for Windows hosting.
     
    Last edited: Jun 27, 2023
  3. JoaoFlora

    JoaoFlora

    Joined:
    Dec 1, 2022
    Posts:
    3
    Thank you for your time and very good answer, that was really nice!

    So to make it totally clear to me, if i create a linux/windows server project (server app) and make it run on a vps (for example) i could connect webgl only as client or even this is impossible because of the reason that web browsers execute webgl apps in a sandbox?
     
  4. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,685
    Hey @JoaoFlora, Mirror actually supports WebGL via it's SimpleWebTransport.
    https://mirror-networking.gitbook.io/docs/manual/transports/websockets-transport

    I'm actually building a new WebGL Multiplayer game myself with Mirror, but I am using a Dedicated Server for all WebGL Clients to connect to.

    If you are sure you want to create a WebGL game where the client also acts as server, yes it is possible... you can accomplish this with Photon Fusion. https://www.photonengine.com/fusion

    Photon offers what's called a Relay Service, which allows clients to find a game that is being hosted by a Master Client, then connects all the clients together. Clients are not really connected to the master client directly, as the Relay Service is handling the connectivity and relaying all the required network messages to all clients. Although there is a-lot of risk with this approach.

    1. Poor Gameplay Experience Due to Increase of Latency through Relay and depending on a Master Client (acting as a server) that could have poor internet connection, or not very good computer specs.

    2. Hacking & cheating is impossible to prevent because the Master Client can modify the entire state of the game at any time effecting all other clients.

    I've created a game like this before, and I can tell you it's not worth it! So I'd certainly would suggest exploring building a WebGL Game with Dedicated Server w/ Mirror or whatever other networking solution you like.

    Feel free to join me on Discord, I recently started a Unity WebGL Multiplayer Game Developers Discord to help support developers like yourself. https://discord.gg/2NhcYqeTaa