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.
Related
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...
}
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
Socket.IO timeout (disconnect) occurs if there are no activity present in the socket, but how is detected that are no activity? I dont find information about that. How works the detection process? For example:
If user closes the website tab, occurs disconnect?
There is any way user loses connection and disconnect will not be executed?
Is possible cache information informs that Im online even if I leave the website?
If you open your browser's developer tools in the network panel, you can filter your requests to ws requests (web socket requests), in there you can see your active web sockets connections. If you choose one connection, you can see the headers, the frames, the cookies and timing. If you choose the frames option you can see what's being sent and received, between your browser and your web socket server.
The next image will make it clear for you, it's chrome's developer tools:
Now in there you can see there are some numbers, basically your browser and your server are doing ping pong. You can read more about these numbers in this answer SocketIO Chrome Inspector Frames
This ping pong is what keeps the socket alive so we know that there are no timeouts. As for the disconnect and the connect I advise you to read more about the WebSockets API, in there you can see there are event listeners for onclose, onerror, onmessage and onopen.
So answering this question:
For example: If user closes the website tab, occurs disconnect? There
is any way user loses connection and disconnect will not be executed?
No, the onclose event will be fired, but even if any cosmic reason the onclose isn't fired you will eventually disconnect due to timeout.
As for your other question:
Is possible cache information informs that Im online even if I leave
the website?
Yes, that's not up to sockets, that's up to you and your implementation. You can keep a list of online users and only update that list from time to time, let's say 10 minutes. You can keep the online users lists and between the time you update your online users list, some of them are already disconnected.
How is it possible to add a 'disconnect' event whenever I leave a page that is intercepted by my single page application engine?
I have certain events set up (kind of like streaming events on YouTube) and each requires its own socket connection. I now set up a single-page engine on Angular that allows switching between these events.
I used to rely on the 'disconnect' event to let me know when someone has left the event, but they don't work anymore - the socket never shuts off if I leave via my single-page navigation.
Is there any way to configure the socket to still disconnect?
My less-than-ideal approach was to create a new instance of the socket connection on a controller of every page to which I can navigate from the event page, and forbid multiple connections.
If anybody has a better idea, I would love to hear it.
Thanks a lot.
You may call it manually since you are in control of page navigation - whenever client is navigating away from one page, call
socket.disconnect();
socket.close();
This will ensure that when client is navigating away from one page the socket gets disconnected before that.
In the initialization for my Event Page, I'd like to create a WebSockets connection via Socket.IO and wake up the page whenever data comes in on that connection.
I suspect this isn't possible and I'll have to use a Background Page, but is there any clever way I can use Event Pages instead so I don't make my users incur the perf hit?
I'm pretty sure you can't do this simply because Event Pages are designed to unload when not in use. Whenever the page unloads, it will close any WebSocket connections. If it never unloads, then you effectively have a persistent background page.