question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stringifying postMessage data vs leaving as Object

See original GitHub issue

Hello!

Feature request incoming:

I noticed that when calling window.ReactNative.postMessage(data) from web, the param (data) passed in must be a string (which is consistent with the docs for this project).

I was wondering what the reason is for stringify-ing/enforcing that a string is passed?

Why not allow web to send Objects, arrays, strings, numbers etc?

I dug in and modified the source code here: https://github.com/react-native-community/react-native-webview/blob/master/ios/RNCWebView.m#L161

By removing String(data) in favor of just `data, I confirmed that we can send objects over the bridge.

For example

Web:

window.ReactNative.postMessage({value: 'Hello, world!', amount: 100});

Native:

<WebView ... onMessage={(event) => {
        const object = event.nativeEvent.data // {value: 'Hello, world!', amount: 100}
	    object.value //'Hello, world!'
        object.amount // 100
    }
}/>

Any reason for explicitly forcing strings?

And would we be interested in a PR to change this behavior 😄 ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

2reactions
chirag04commented, Nov 22, 2019

postMessage as per mdn link below supports a broad variety of data objects for the message argument. It would be nice to not enforce string only for postMessage because it will break with a third-part site in webview integration.

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Data to be sent to the other window. The data is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself. [1]

0reactions
weraaaazcommented, Feb 28, 2020

Can we reopen this? It causes problems with third-party sites 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stringifying postMessage data vs leaving as Object #1040
Hello! Feature request incoming: I noticed that when calling window.ReactNative.postMessage(data) from web, the param (data) passed in must ...
Read more >
IE passes an empty event object with PostMessage
1 Answer 1 ... IE does not implement window.location.origin . And JSON.stringify() is apparently ignoring properties whose value is undefined ( ...
Read more >
High-performance Web Worker messages
It's actually faster to JSON.stringify() then postMessage() a string than to postMessage() an object. This insight was further confirmed by ...
Read more >
Is postMessage slow? - surma.dev
The data shows that the complexity of the object is a strong factor in how long it takes to serialize and deserialize an...
Read more >
HTML5 window.postMessage - David Walsh Blog
postMessage allows for sending data messages between two windows/frames across domains. Essentially window.postMessage acts as cross-domain AJAX ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found