Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (49 sloc) 1.92 KB

More modern proposal here:

partial interface Navigator {
  Promise<any> connect(DOMString url, MessagePort port);

The navigator.connect attempts to establish a connection with a service. A service could be represented by a Service Worker. The port parameter is a MessagePort that is transferred to the service.

The service is identified by the url parameter.

For example, here's how I would try to talk to the's Service Worker:

var contactsChannel = new MessageChannel();
var contacts = contactsChannel.port1;
contacts.onmessage = function(e) {
// I can start sending messages right away.

navigator.connect('', contactsChannel.port2).catch(function() {
  console.log('we failed to connect, maybe next time?');

The failure of promise indicates that the connection is not possible. The actual reason (denied, service unavailable) is not communicated back.

Receving/Handling Connect Requests

If a Service Worker is installed and is able to handle the url within its scope, it receives a connect event that uses the ConnectEvent interface:

interface ConnectEvent : Event {
  readonly attribute DOMString origin;
  MessagePort port;
  void reject();

For example, here's how the attempt above to connect to the would be handled by its Service Worker:

/// in Service Worker
this.addEventListener('connect', function(e) {
  if (e.origin == '') {
    // yes, I will be happy to handle your request.
    var port = e.port;
    // port is given to you only after you accept the request.
    port.onmessage = function() {
      // I am listening...
  } else {
    // no, you evil basterd.