13

I am developing a chrome extension that pulls large public keys from webpages. I need to know if I will run into any limitations that I can not find on googles website that relate to their message passing API.

Thanks!

Siddartha
  • 175
  • 1
  • 8

1 Answers1

23

When you use chrome.runtime.sendMessage, the message is serialized, and sent from the sender's process (e.g. the tab containing the content script) to the extension's process (the background page) in one go. The hard limit of the IPC message is 128 MB. If you send anything bigger than that, the sender's process will be terminated.

Now, the message size being 128 MB does not mean that you can send a JavaScript string of length 134,217,728, because the message itself also needs some space to store metadata. And the number of characters in a JavaScript string is not necessary the number of bytes (assume that on average, a JavaScript string consisting of arbitrary characters require 2 bytes storage per character). This brings down the maximum limit to 50 MB.

Although 50 MB is technically acceptable, you should not try to send so much data. Here is a screenshot of Chrome's task manager right after starting up the browser and sending 50MB of data. The picture shows that it took 5 seconds to get the message from the tab to the background page, and that the browser and tab process both use about 300MB and the extension process 663MB (where the initial values were 32, 17 and 22 MB, respectively).

Messages of a few megabytes are not a problem, however.

task manager: 293MB browser, 281MB tab, 663MB extension

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Great answer. However on a 32-bit Chrome a 100MB string is transmitted successfully here, not 50MB, and it took only about 1 second. – wOxxOm Aug 10 '15 at 20:23
  • 1
    @wOxxOm I said that the maximum value for an *arbitrary* string is 50MB. Sending 100MB of ASCII strings (8-bits per characters) will work fine, but otherwise the data is likely encoded as UTF-16 (or UCS-2). When I tried to transfer the contents of a binary file that had a size of 55MB, the tab was killed. – Rob W Aug 10 '15 at 20:31
  • i dont think its insane. for example i have an extension that sends an sql query to background which sends results. ideally the code shouldnt need to worry between normal and huge results. but i see that it does need to diferenciate :( – Zig Mandel Aug 10 '15 at 22:41
  • 1
    @ZigMandel Do you really need to send everything at one go? Can't you format the results and then send the small result to the content script? Or split up the result in parts and use `chrome.runtime.connect` to exchange small messages over the port? Or, completely remove the need for extension message passing by inserting an iframe in the page and performing the database query from there. – Rob W Aug 11 '15 at 09:31
  • not practical in my case. i could pass the result in parts thou, but i thought this was already handled my messaging. – Zig Mandel Aug 11 '15 at 13:01
  • Thanks for the analysis! this is a big help.The keys wont ever come close to that size but its good to know! – Siddartha Aug 13 '15 at 20:50
  • I found that on sending an object with a ~150 keys (each one with ~500 bytes of data) receiver gets empty. – Vitaly Zdanevich Sep 11 '18 at 15:44