-
Notifications
You must be signed in to change notification settings - Fork 312
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
Clients & postMessage #609
Comments
@mvano @johnmellor @beverloo @mounirlamouri - in case you care from a push point of view |
I think on both sides we want the That puts into question whether |
I don't think the snapshot nature of The client is a static representation of the window/sharedworker/dedicatedworker. Client exists because Now that |
The problem is that if (Having |
Idea: we provide no built-in messaging API and require usage of |
Hah, I had the same thought. Does anyone implement Even with I take it you're dead against |
Firefox implements it. If there's some form of buffering it could work for service workers I think although that might be somewhat tricky. (Though that needs to be solved either way.) I'm not sure what you mean by |
I see |
@annevk can you sum up why having |
|
I disagree that the Will arrange a call to go through this. |
I think it would make more sense for someone to sit down with @Hixie and sort this through. |
@Hixie some background: A page can navigator.serviceWorker.controller.postMessage("Hello");
In the ServiceWorker, clients.matchAll().then(clients => {
clients[0].postMessage("Hello");
}); …as a method for the ServiceWorker to communicate with a client. This is where there's disagreement. My initial thinking was, for windows, Alternatively, Other possibilities@annevk suggests dropping There's also the idea of stashing message ports within ServiceWorker registrations https://gist.github.com/mkruisselbrink/536632fcd99d45005064 - but this comes with all the GC observation of |
We're still stuck here. In case it helps, here are the requirements:
Use cases:
|
A client (C) is associated with zero or more I think we need to reconsider making these objects live (and therefore unique per global). When live message listeners could be registered at SW and C creation time and things would work. (This would mean SW is not always woken up by an event dispatched on the global.) Making them live would require some kind of synchronization event when a new C or SW presents itself to the other side. It would also mean that any state on User agents can still optimize by creating these collections lazily when they are accessed (or assumed to be accessed based on experience running the code). |
Blink has already shipped Client.postMessage. We plan to add a console message warning the developer that the API may change. |
Update: We're circling on a model where
This changes what Chrome has already shipped, but it gives us a sensible place to land messages in SharedWorkers, and doesn't overload I don't think this can be specced in terms of ports, as it isn't 1:1 (both @jungkees do you have any time to have a go at specing this? I'm snowed under with talk-writing :( |
Just to inform implementation, do we have thoughts (to pass on to @Hixie) about updating the IDL definition for Currently it is (The spec for |
Tracked at https://code.google.com/p/chromium/issues/detail?id=459413 for Blink. |
It doesn't really sit well with me that there's a lack of consistency in how service workers are exposed to clients vs how clients are exposed to service workers. An alternative I proposed was that we make them all live (rather than just one side), updates based on events, and let the message events bubble up to |
Code like #588 (comment) becomes unworkable if we start putting values behind promises.
From where? SW instances aside from the one controlling the document are behind promises. |
@mounirlamouri Anne is suggesting the client objects should be 'live'. We can't put their state properties behind promises, else iterating over them to find which client to focus becomes unmanageable. Having client properties update live sounds like a performance issue to me, or is it not as bad as I think? We could make the values update lazily, making only rough guarantees about accuracy. Or we could stick with the snapshots and give clients an |
Additionally, I think we should make This also allows us to reject posts to "asleep" tabs. #626 |
I was suggesting we keep that live as well, updating based on tasks.
How does that work if they are live? |
Making the clients live is going to have a lot of implications on the current design. .focus() would no longer return a new Client object because it wouldn't really make sense. Also, implementations will have to keep track of window objects that are clients and update the service workers that know about them - which, as you pointed - might be a performance issue. Also, with Chrome shipping the current status of the specification, going backward might come with its share of compat issues. |
Well, the problem is that currently there's not much design to speak of. E.g. elsewhere @jakearchibald argues how useful it is to |
@annevk @slightlyoff & I had a meeting last night about this stuff. The conclusions were:
The |
I think we will have to subclass or create our own event type for this. |
I think the |
Why not simply get that changed? If @Hixie doesn't deal with the extensibility he could even allow "any" there. Doesn't matter much. |
What about the case when the sender is not controlled by the service worker? Should the source still be an instance of WindowClient/Client? navigator.serviceWorker.register({some options}).then(function(registration) {
registration.active.postMessage();
}); Assuming the registration has an active worker, the registering window can post messages to the worker without being controlled by it. |
@jakearchibald you mentioned that it's desirable to have postmessage return a promise in #609 (comment) It's unclear to me if this is indeed part of the consensus reached in #609 (comment) |
I think any instantiated Yes, the |
@jakearchibald In case of the uncontrolled clients gotten from |
@KenjiBaheux we did not discuss a return value for |
@annevk wrote:
Not really, there are many reasons why the postMessage could fail, such as the user having closed the tab, or JS having navigated the tab to a non-same-origin URL. It's #626 which would arguably expose evicted tabs (which I think is what you refer to by GC). |
I don't think this exposes GC, it's the closing of a window it exposes, and On Wed, 25 Feb 2015 11:02 John Mellor notifications@github.com wrote:
|
What if the user agent implements "fast back"? |
Good point. But as @johnmellor says, this doesn't give you the difference between a salvageable but unloaded document and a user-closed one, so I don't think it counts as GC exposure. |
Yes. You can already get hold of these clients via |
Yep! And |
Spec'ed Notes:
|
I had a hard time following the decision making process to its conclusion here. Will it be possible to get reference to, and message (unicast not broadcast) the client which originated the fetch event? It's my most important use case. |
Yes, you can do this: self.addEventListener("fetch", function(e) {
e.client.postMessage("To the client initiated this fetch.");
// It lands on the client's global object's navigator.serviceWorker.onmessage
}); And this messaging is a unicast. |
Closing. |
A client can message a particular ServiceWorker via:
I guess
messageEvent.source
would be a client, but whereclient.postMessage
goes isn't properly defined. This is also a problem for clients created byclients.getAll
.In my ignorance, I thought
client.postMessage
would go toglobal.onmessage
, butSharedWorker
doesn't have that, and I hadn't really appreciated thatwindow.onmessage
was only for window-to-window communication.So yeah, if a ServiceWorker sends a client a message, where does it land?
We could have
navigator.serviceWorker.onmessage
as the target for messages from a SW to this specific client, wheremessageEvent.source
is an instance ofServiceWorker
, allowing state inspection &postMessage
back.The text was updated successfully, but these errors were encountered: