2

I'm trying to build a Chrome app and/or extension to communicate with a serial device (using https://developer.chrome.com/apps/serial), to use for showing data from the device on a webpage.

I've created an app that can read messages from my device but I now need to get those messages to my webpage. My plan was to create an extension that communicates with my app and then updates my webpage. My extension sends messages to the app using "chrome.runtime.sendMessage" and my app then responds with messages containing information from my device.

This works fine when the app is running but when I close my app it stops listening for new messages. Is there any way to always keep my app listening to new messages?

The reason I've created an app (and not just an extension) from the start is because extensions are not allowed to communicate with serial devices. Is there some other way than building both an app and an extension I can achieve what I want?

Update:
I just realized that I can send messages directly to the app from my webpage using "chrome.runtime.sendMessage", so I guess that the extension isn't needed. I however still need the app to be listening to messages all the time.

gusjap
  • 2,397
  • 5
  • 24
  • 38

1 Answers1

2

Problem solved :)

I moved all the logic to the script (background.js) that was being set as the background script in the manifest file:

"app": {
    "background": {
      "scripts": [ "background.js" ]
    }
  },

In background.js I setup everything (including the message listener) on chrome.app.runtime.onStartup.addListener(function() { .

gusjap
  • 2,397
  • 5
  • 24
  • 38
  • 1
    It's easier to grasp this concept if you use the terminology from the documentation (https://developer.chrome.com/extensions/event_pages). In Chrome Apps there are no background pages; rather, they're called "event pages," and that's because they're the pages that are designed to listen for events. The system takes care of resurrecting evicted event pages for you when it has something to tell them. – sowbug Apr 24 '14 at 17:30