Equivalent of SharedWorker Port in ServiceWorker?

Lewis

SharedWorker uses Message Port to exchange messages with ParentWorker.

ParentWorker

var port = new SharedWorker('/worker.js').port;
port.onmessage = function(e){
    console.log(e.data);
};
port.start();
port.postMessage("Hello Shared Worker. I'm Mr. Tab");

SharedWorker (worker.js)

onconnect = function(e){
    var port = e.ports[0];
    port.onmessage = function(e){
        console.log(e.data);
    };
    port.start();
    port.postMessage("Hello Mr. Tab. I'm Shared Worker.");
};

What about Service Worker? Is there anything similar to SharedWorker Port?

Lewis

@Jaffa The Cake's answer is just a simple in and out messaging channel, not an exact equivalent of SharedWorker Port. At this time, there is no built-in interface which is similar to SharedWorker Port in ServiceWorker. Message Channel API is mandatory. Here is the one.

yourapp.js

var mc = new MessageChannel();
mc.port1.onmessage = function(e){
    console.log(e.data);
};
var sw = navigator.serviceWorker;
sw.register('/sw.js',{scope : '/'}).then(function(){
    console.log("Service Worker is successfully registered.");
}).catch(function(er){
    console.log("Registration failed.");
});
sw.ready.then(function(reg){
    reg.active.postMessage('',[mc.port2]); //initialise the messaging channel
    mc.port1.postMessage("Hello Service Worker. I'm Mr. Tab.");
});

sw.js

//equals to onconnect in Shared Worker
onmessage = function(e){
    var port = e.ports[0];
    port.onmessage = function(e){
        console.log(e.data);
    };
    port.postMessage("Hello Mr. Tab. I'm Service Worker.");
};

Note: This code is tested on Chrome 43 Beta.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sending messages from SharedWorker to SharedWorker

From Dev

Sending messages from SharedWorker to SharedWorker

From Dev

IO Completion port Linux equivalent

From Dev

VPN equivalent to SSH port forwarding?

From Dev

IO Completion port Linux equivalent

From Java

Nginx variable equivalent of: $scheme://$host:$port

From Dev

An equivalent of ssh port-tunneling for Windows servers?

From Dev

Why GitHub use SharedWorker for WebSocket

From Dev

registerProtocolHandler and ServiceWorker

From Dev

Why did Safari drop support for SharedWorker?

From Dev

Failed to register a ServiceWorker: ServiceWorker cannot be started

From Dev

Protocol Buffers - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream.getBytesUntilLimit() exist?

From Dev

Protocol Buffers - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream.getBytesUntilLimit() exist?

From Dev

Where are ServiceWorker data stored?

From Dev

To check if ServiceWorker is in waiting state

From Dev

Page URLs in ServiceWorker scope

From Dev

ServiceWorker source headers

From Dev

ServiceWorker and Push Notification Payload

From Dev

RequireJS within ServiceWorker

From Dev

Chrome ServiceWorker postMessage

From Dev

ServiceWorker and Push Notification Payload

From Dev

ServiceWorker source headers

From Dev

ServiceWorker Cache on Install

From Dev

Ionic PWA serviceworker - no CORS

From Dev

Why does the SharedWorker onConnect event have a Ports array?

From Dev

socket.io Websocket connection inside a HTML5 SharedWorker

From Dev

Difference between serviceWorker.getRegistration and serviceWorker.ready.then

From Dev

DOMException: Failed to register a ServiceWorker: ServiceWorker script evaluation failed

From Dev

Serviceworker conflict with HTTP basic auth?

Related Related

  1. 1

    Sending messages from SharedWorker to SharedWorker

  2. 2

    Sending messages from SharedWorker to SharedWorker

  3. 3

    IO Completion port Linux equivalent

  4. 4

    VPN equivalent to SSH port forwarding?

  5. 5

    IO Completion port Linux equivalent

  6. 6

    Nginx variable equivalent of: $scheme://$host:$port

  7. 7

    An equivalent of ssh port-tunneling for Windows servers?

  8. 8

    Why GitHub use SharedWorker for WebSocket

  9. 9

    registerProtocolHandler and ServiceWorker

  10. 10

    Why did Safari drop support for SharedWorker?

  11. 11

    Failed to register a ServiceWorker: ServiceWorker cannot be started

  12. 12

    Protocol Buffers - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream.getBytesUntilLimit() exist?

  13. 13

    Protocol Buffers - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream.getBytesUntilLimit() exist?

  14. 14

    Where are ServiceWorker data stored?

  15. 15

    To check if ServiceWorker is in waiting state

  16. 16

    Page URLs in ServiceWorker scope

  17. 17

    ServiceWorker source headers

  18. 18

    ServiceWorker and Push Notification Payload

  19. 19

    RequireJS within ServiceWorker

  20. 20

    Chrome ServiceWorker postMessage

  21. 21

    ServiceWorker and Push Notification Payload

  22. 22

    ServiceWorker source headers

  23. 23

    ServiceWorker Cache on Install

  24. 24

    Ionic PWA serviceworker - no CORS

  25. 25

    Why does the SharedWorker onConnect event have a Ports array?

  26. 26

    socket.io Websocket connection inside a HTML5 SharedWorker

  27. 27

    Difference between serviceWorker.getRegistration and serviceWorker.ready.then

  28. 28

    DOMException: Failed to register a ServiceWorker: ServiceWorker script evaluation failed

  29. 29

    Serviceworker conflict with HTTP basic auth?

HotTag

Archive