Listen in a websocket in Chrome extension manifest V3 - google-chrome-extension

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?

Related

sendMessage from a service worker to popup

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.

Close Native Host when Chrome is closed by the user

I am working on a Chrome extension that uses a C++ Native Host. In a background.js script (persistent set to false), using chrome.onstartup event, I create the connection to the C++ Host.
I want my Host to be running for as long as the user is actively using Chrome.
If I close all my current Chrome tabs, independent Chrome processes still appear in the "Background section" of the Task Manager (including my Host process that must be explicitly killed).
I understood that the user can configure the Chrome not to run background processes, but can I design my extension to kill the Host process(disconnect the port) when the user closes all Chrome tabs?
Moreover, the problem becomes more serious if I disable the extension. The Host process becomes a detached process in the background. If enable the extension again, kill all Chrome processed and restart Chrome (as my extension connects to Host on startup of Chrome), I will have multiple Host processes.
When Chrome terminates or your extension is unloaded, it will send -1 message to your native messaging host. You will have to check for that value, assuming your native messaging host is written in C++ then this is what you should do:
int read_char = ::getchar();
if (read_char == -1) {
// Do termination work here...
}

(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

Azure Function web socket client

Is there any way to have an Azure Function listen for events from a remote web socket and handle messages as their come over?
I have looked at the current documentation and samples but haven't found an answer.
Web sockets are not currently an event trigger that is supported by Azure Functions. Your only option would be to have another application that listened to the web socket and placed messages on a queue or hit an HTTP triggered function, but at that point the listening application might as well handle the incoming message itself.
The documentation here shows the current list of supported function triggers:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings

Using RabbitMQ to capture web application log

I'm trying to setup RabbitMQ to take web application logs to a log server.
My log server will listen to one channel and store the logs that comes in.
There are several web applications that need to send info to the log server.
With many connections (users) hitting the web server, what is the best design to publish messages to RabbitMQ without locking each other? Is it a good idea to keep opening a new connection to the MQ for each web request? Is there some sort of message queue pool?
I'm using IIS for a web server.
I assume you’re leveraging the .NET framework to build your application, given that it’s hosted in IIS. If so, you can also leverage Daishi.AMQP, which has a built-in QueuePool feature. Here is a tutorial that outlines the mechanism in full.
To answer your question, you should initially establish a connection to RabbitMQ from your application server. You can then initialise a Channel (a process that executes within the context of the underlying connection) to serve each HTTP request. It is not a good idea to establish a new connection for each request.
RabbitMQ has a build in queue feature. It is well documented, have a look at the official docs: http://www.rabbitmq.com/getstarted.html

Resources