Is there an easy way to run Firebase in a web worker?

我是研究僧i 提交于 2019-12-05 12:38:57

Easy, depends on your values for easy. Possible, definitely.

I used microdom to get it working. Here's a rough/dirty version, bundle as makes sense to you.

In your worker:

importScripts("http://raw.github.com/tmpvar/microdom/master/microdom.min.js");
var window = microdom('<html></html>');
var document = window;
importScripts("https://cdn.firebase.com/js/client/2.4.2/firebase.js");

var ref = new Firebase(input.firebase_endpoint);
// This ref now works, authenticates, etc.  You can use .notify() to pass info to the main thread, etc.

ref.on("value", function(snapshot) {
    output.notify(snapshot.val());
});

I have a working open source implementation that puts Firebase into a worker: https://github.com/pkaminski/fireworker. It can use both plain and shared workers (where supported) and integrates extra features from other Firebase add-on libraries. It emulates the Firebase API and automatically bridges calls from the main page to the worker, so your existing code should more-or-less continue to work as-is. It also has some non-trivial limitations, as of this writing:

  • Only supports Firebase SDK 2.4.2. Could probably be adapted to SDK 3.x but it's going to be harder since the source code is not available.
  • Interactive authentication methods don't work; calling authWithPassword, authWithOAuthPopup, or authWithOAuthRedirect will throw an exception. Instead, use your own server for login and call authWithCustomToken.
  • Item priority is not currently implemented. Because priority is stored out of band it's difficult to support efficiently, and Firebase folks have indicated that it's essentially deprecated anyway.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!