Version: 2021.3
Language : English
Configure a WebGL Canvas size
WebGL performance considerations

WebGL networking

You can use networkingThe Unity system that enables multiplayer gaming across a computer network. More info
See in Glossary
in Web in the following ways:

  • Use the UnityWebRequest class.
  • Use WebSockets or WebRTC from JavaScript to implement your own networking. You can’t use any .NET networking classes within the Web platform because JavaScript code doesn’t have direct access to internet protocol (IP) sockets to implement network connectivity. Specifically, Web doesn’t support any .NET classes within the System.Net namespace.

Use the UnityWebRequest class in WebGL

Unity supports the UnityWebRequest class in WebGLA JavaScript API that renders 2D and 3D graphics in a web browser. The Unity WebGL build option allows Unity to publish content as JavaScript programs which use HTML5 technologies and the WebGL rendering API to run Unity content in a web browser. More info
See in Glossary
. To implement the UnityWebRequest class, Unity uses the JavaScript Fetch API, which uses the browser to handle web requests. This imposes security restrictions on accessing cross-domain resources.

If you send a web request to a server other than the one that hosts the Unity content, the server you’re sending it to must authorize the Unity content.

To access cross-domain web resources in WebGL, the server you are trying to access needs to use cross-origin resource sharing (CORS) to authorize cross-domain web resources.

If you try to access content using UnityWebRequest, and the remote server doesn’t have CORS set up or configured correctly, an error like the following appears in the browser console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myserver.com/. This can be fixed by moving the resource to the same domain or enabling CORS.

The server needs to add Access-Control headers to the http responses it sends out, to indicate which web pages have permission to read that information from a web browser.

For a demonstration of how to add Access-Control headers that allow Unity WebGL to access resources on a web server from any origin, refer to the following example. This example includes common request headers and allows the GET, POST or OPTIONS methods:

"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Origin": "*",

UnityWebRequest downloads

Don’t use code that blocks a UnityWebRequest download, such as this:

while(!www.isDone) {}

You can’t block the thread to wait for a UnityWebRequest download to finish otherwise your application freezes. Because WebGL is single threaded, and the fetch API in JavaScript is asynchronous, your download might not finish unless you return control to the browser. Instead, use a Coroutine and a yield statement to wait for the download to finish. For more information, see Examples of coroutines using UnityWebRequest.

Using Unity Multiplayer

Unity Multiplayer enables communication via the WebSockets protocol. For more information, refer to NetworkServer.useWebSockets.

Use WebSockets or WebRTC from JavaScript

WebGL doesn’t allow direct access to IP sockets, but you can use WebSockets or WebRTC (the two most common networking protocols supported by browsers) to get around this. While WebSockets are widely supported, WebRTC allows peer-to-peer connections between browsers and unreliable connections. Unity doesn’t have a built-in API that allows you to use WebSockets or WebRTC, but you can use a JavaScript plugin to implement this. You can find plugins that implement WebSocket networking on the Unity Asset Store.

Limitations of WebGL networking

There are a few networking features that WebGL platform doesn’t support.

No support for native socket access

WebGL platform does not support native socket access because of security limitations within browsers. Therefore, WebGL also doesn’t support features like UnityEngine.Ping (ICMP).

Configure a WebGL Canvas size
WebGL performance considerations