sendMessage from a service worker to popup - google-chrome-extension

How can a service worker use sendMessage to communicate with a script running in the popup?
In manifestv3:
content scripts use chrome.runtime.sendMessage to talk to the service worker.
the service worker uses chrome.tabs.sendMessage to talk to content scripts.
content scripts use chrome.runtime.sendMessage to talk to the popup.html's popup.js
popup.js uses chrome.tabs.sendMessage to talk to content scripts.
popup.js uses chrome.runtime.sendMessage to talk to the service worker.
In this triangle, the only side missing is between popup.js and the service worker.
How does the service worker message popup.js?
But using chrome.runtime.sendMessage results in "Could not establish connection. Receiving end does not exist."
And using chrome.tabs.sendMessage requires chrome.tabs.query, which doesn't seem to return popup.html at all.

Related

Listen in a websocket in Chrome extension manifest V3

For a private extension we need to run one persistent websocket.
In manifest V2 the process was managed in the background script. But in manifest V3 the background script has been canceled in favour of service worker. Is it possible in V3 to have an ongoing process that listens to a websocket connection?

Dispatching redux actions inside custom serviceWorker

I want to perform actions on push received from server. I have a custom service worker which has the event listener for push. Now i want to dispatch redux actions when i recieve the push from server. My custom service worker lives inside public folder at the moment. And i am unable to import the store inside this file. Any help would be highly appreciated. Thanks!
It is not possible to dispatch Redux actions inside your Service Worker code.
However, what you actually want to do, is communicate using the postMessage API. Using postMessage, you can send the browser JS context a message when the SW receives a push from the server. You can find more info eg. here https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage.
Why is it like this?
This is a consequence of the different execution contexts. Your normal JS code (React, Redux, actions etc.) run in the browser context where as your Service Code runs in the SW context. Those two contexes don't share ANY variables or state. For that reason, you cannot call any functions (eg. Redux actions) inside your SW code. You need to communicate between the two contexes and then, upon receiving a message in the browser context, call whatever functions you wish.

(How) Can a Chrome Extension listen for messages from my server?

My Chrome Extension's background page is set up as an event page, i.e., most of the time it is asleep unless some registered event listener wakes it up.
I'd like to be able to occasionally send messages from my server to the event page of an individual user of my extension. They should not necessarily show up as a desktop notification, it would rather be up to the background script to decide what to do with any incoming message. It might very well store some information in localstorage for example. If the user client was offline at the moment the message is being sent, it would ideally be delivered once it comes back online.
I'd like to avoid polling my server at regular intervals every time the background script is awake, though that would be an obvious solution.
My question is therefore if it is possible to register a special kind of event in my event page so that it wakes up and triggers some functionality once there's an incoming message from my server. Ideally, the server message would not be a general broadcast to all my users, but rather a targeted message to a specific user.
What options do I have?
I read about service workers and their Push API but it seems they are only slowly being rolled out to Chrome Extensions. I am not sure if they are ready for the browser's stable release yet and didn't find any documentation on how they work with extensions.
I also read a bit about Google Cloud Messaging but it is deprecated in favor of a new costly Firebase solution.
Service worker functions like a proxy server, allowing you to modify requests and responses, replace them with items from its own cache, and more. While Chrome has its own approach to caching/installing the resources need to display a Chrome Extension. Therefore, there will be an error when you will attempt to intercept the registration of a service worker to a Chrome Extension.
See for more information:
Introduction to service worker
Service Worker script errors if run in chrome extension
See related SO post:
Chrome Extensions with service worker receiving push notifications

Chrome Extension Event Page communicating with external native host

I'm building a Chrome Extension that, through a native host, should simulate a key press. I can connect to my native host to check that it's there etc in the popup I've created, but in my event page script, should I just connect to the native host? It says on the Chrome Extension Developer Page that:
Event pages are loaded only when they are needed. When the event page is not actively doing something, it is unloaded, freeing memory and other system resources.
So if I want it to run "forever", i.e. listen to the native host and simulating a key press whenever it gets a "ping", how should I do that? The page says I should create events for that, but do I just listen to the port then?
Thanks,
Johan
As long as there is an open port opened with connect(), an Event page will not shut down.
If you think it's going to be like that most of the time, don't bother with Event pages and put "persistent": true (or nothing) to make a normal background one.

WCF net.TCP traffic clarification

I have implement a WCF service that implement call back.
I have client web app connect to WCF via HTTP API
and Remote client app, that run in windows OS and connect to WCF using net.TCP include callback support.
now client send actions to remote and remote execute them and return status by callback return value.
I have a thread, that every 2min send ImAlive (call bool WCF.ImALIVE(machineID)) to keep the net.TCP alive if there is no activities.
My question:
if I get callback action from client, and while remote execute it ImAlive thread wakeup and call WCF.ImALIVE, is there will be any issue of block or deadlock or time out?
It depends on the ServiceBehavior ConcurrencyMode, read more here http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and.
Here is also a duplicate of what you are trying.
Use WCF Service with basic authentication user
You can see this related question also, but related to instancing on server side.
Сan I use PerCall instancing and Reentrant concurrency in the same service?

Resources