Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to communicate using postMessage with ServiceWorkers #34

Open
arcturus opened this issue May 12, 2015 · 4 comments
Open

Need to communicate using postMessage with ServiceWorkers #34

arcturus opened this issue May 12, 2015 · 4 comments

Comments

@arcturus
Copy link

While testing integration of ServiceWokers with threads.js as a mechanism to communicate with SW (see: https://github.com/arcturus/sww-render-cache).

We realized that, following the spec, the UA can kill a SW at any moment. Which mean that the broadcast communication channel won't respond to any message (as the server side in the SW has been already killed).

Following the spec, we also see that a SW can be wake up again if we send a postMessage to it, so the communication mechanism that we should have for SW should be postMessage to be sure that the SW will be alive when we perform the communication.

Another point to take into account is that with a postMessage we will relaunch the sw, so if there was already some metadata set up, needed for the communication, some how we will need to reuse it.

@wilsonpage
Copy link
Contributor

In order for this to work the Client will need to have a direct handle on the SW thread.

var thread = threads.create({
  target: myServiceWorker,
  type: 'serviceworker'
});

var client = threads.client('render-cache', { thread: thread });

We will also have to make some changes in the Client code so that if we have a thread reference we should communicate with it using .postMessage() and not bother creating a BroadcastChannel at all. We should probably also make some similar tweaks in Service.

@wilsonpage
Copy link
Contributor

Perhaps we could streamline the above code example to make it easier for the user.

var client = threads.client('render-cache', { target: myServiceWorker });

@wilsonpage
Copy link
Contributor

At first glance, this is all do-able 😄

@arcturus
Copy link
Author

Getting a reference to the SW can be like:

var sw = null; navigator.serviceWorker.getRegistration().then(function(registration) { sw = registraction.active; });

Which means that reference can be obtained from a window object, no worker or sharedworkerd can get a reference to the active serviceworker. (As a document has a relation with a SW, since the scope of the serviceworker indicates which documents are handled by each sw).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants