RTDB Server-sent Events with Unity WebGL Builds

110 views
Skip to first unread message

Alex Ferrier

unread,
Jan 27, 2020, 1:28:10 PM1/27/20
to fireba...@googlegroups.com
We have a Unity application that makes extensive use of RTDB for realtime collaboration, and would like to embed a small subset of it in a Unity WebGL Build. 

Unfortunately, the official Firebase JavaScript SDK can't sanely be integrated in with a Unity WebGL app (because it's massive.) We would like to port the parts of the application that we want in the WebGL build to simply use REST RTDB with server-sent events. 

However it appears that specifying text/event-stream results in Unity believing it never receives a response. (The small example posted below will spin forever in the loop, without req.IsDone ever returning true.) (The same infinite-execution-with-no-data problem also happens when using PostMan to test APIs, although using curl works fine.) I'm not a web programmer by background, but is there a way that the Firebase team knows of that we can access RTDB *streaming* results via Unity WebGL? 

Note that the example below works fine (but does not support streaming events) in WebGL if you simply comment out the line where we SetRequestHeader. Also, the application below works fine in Editor as posted in every platform except for WebGL, even the crazy edge case known as UWP. 

We'd be really grateful for any advice. 



using System.Collections;
 using UnityEngine;
 using UnityEngine.Networking;

 public class Test : MonoBehaviour
 {
     public IEnumerator TestCoroutine()
     {
        Debug.Log("TestCoroutine()");

         string firebaseHost = "https://fir-testdatabase-REMODEDID.firebaseio.com";
         UnityWebRequest req = UnityWebRequest.Get(firebaseHost + "/.json");
         req.SetRequestHeader("Accept""text/event-stream");
         var response = req.SendWebRequest();

         int bytesSoFar = 0;

         while (!req.downloadHandler.isDone)
         {
             if (req.downloadHandler.text.Length != bytesSoFar)
             {
                string allText = $"Bytes: {req.downloadHandler.text.Length} : " + req.downloadHandler.text;

                Debug.Log(allText); 
                
                bytesSoFar = req.downloadHandler.text.Length;
             }

             yield return null;
         }

        Debug.Log("IsDone");
        Debug.Log("Final value to text: " + req.downloadHandler.text);
     }

     // Use this for initialization
     void Start()
     {
         StartCoroutine(TestCoroutine());
     }
 }


Reply all
Reply to author
Forward
0 new messages