Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables communication between the webview and RN #7020

Closed
wants to merge 2 commits into from

Conversation

odino
Copy link
Contributor

@odino odino commented Apr 17, 2016

Android devs have been relying for ages on intercepting custom HTTP
calls made by the webview, to map them to native actions,
like here.

Say that we have a "close" button in our web interface. We first detect
if we're running on web or within a webview -- if on web, we simply call window.close().
If we're running within the app things get a bit more complicated, but we can
simply call app://exit and the app will be able to intercept this call and
close the webview for us.

Now, this PR adds the ability of doing it on RN. You basically specify a
protocol for messages that should be forwarded to RN (ie. rn) and then,
when your webview loads a resource from rn://xyz the onMessage callback
will be called. You can access the URL etc etc through event.nativeEvent.url.

Example:

let onMessage = (event) => {
  let {url} = event.nativeEvent;

  if (url.endsWith('exit')) {
    BackAndroid.exitApp(); // just sayin'...
  }
}

return <Webview
  source={...}
  messageProtocol={'myApp'}
  onMessage={onMessage}
>

Or something of that sort. Communicating between the webview and RN / the ability
to intercept resources loaded within the webview has been already discussed but I
feel we need a very simple implementation rather than creating a full-blown
messaging pattern which will just hurt (maintainability, complexity) in the long run.

Also, my Java is pretty rusty ;-)

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @odino, @mkonicek and @nicklockwood to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 17, 2016
@odino
Copy link
Contributor Author

odino commented Apr 17, 2016

My biggest doubt here is that this guy is android-specific. I think we already have something working on IOS so I would prefer to see both working in the same way. Anyhow, let me know what you guys think.

@odino
Copy link
Contributor Author

odino commented Apr 17, 2016

Saw @astreet comments on #6478 (comment) and I agree that regex patterns might be a good, viable solution. Though, what if we need to run cb when a pattern is matched?

import com.facebook.react.uimanager.events.RCTEventEmitter;

/**
* Event emitted when there is an error in loading.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Javadoc correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, sorry -- copypasta :)

@mkonicek
Copy link
Contributor

In the example, you have: messageProtocol={'myApp'}. Does the WebView load a myApp:// URL? Can you provide a full example please?

@mkonicek
Copy link
Contributor

Saw @astreet comments on #6478 (comment) and I agree that regex patterns might be a good, viable solution. Though, what if we need to run cb when a pattern is matched?

Can you elaborate please? What do you mean by "what if we need to run cb when a pattern is matched?"?

@odino
Copy link
Contributor Author

odino commented Apr 24, 2016

This is what I meant -- we could pass some object like this:

let routesToIntercept = {
  'myApp://some-action': () => console.log(123),
  'myApp://*': () => console.log(456)
}

return <Webview
  routesToIntercept={routes}
/>

and run the cbs based on the route we intercept. But I'm not 100% sure on how to do it in RN -- I might need some help / hints there.

The problem I see with my PR is that IOS / android implementations will vary quite a bit, which sucks. So Im open to consider changing the implementation in favor of something that can work longer term. Let me know your thoughts.

Android devs have been relying for ages on intercepting custom HTTP
calls made by the webview, to map them to native actions, [like here](https://www.caphal.com/android/communication-between-application-and-webview/).

Say that we have a "close" button in our web interface. We first detect
if we're running on web or within a webview -- if on web, we simply call `window.close()`.
If we're running within the app things get a bit more complicated, but we can
simply call `app://exit` and the app will be able to intercept this call and
close the webview for us.

Now, this PR adds the ability of doing it on RN. You basically specify a
protocol for messages that should be forwarded to RN (ie. `rn`) and then,
when your webview loads a resource from `rn://xyz` the `onMessage` callback
will be called. You can access the URL etc etc through `event.nativeEvent.url`.

Example:

``` javascript
let onMessage = (event) => {
  let {url} = event.nativeEvent;

  if (url.endsWith('exit')) {
    BackAndroid.exitApp(); // just sayin'...
  }
}

return <Webview
  source={...}
  messageProtocol={'myApp'}
  onMessage={onMessage}
>
```

Or something of that sort. Communicating between the webview and RN / the ability
to intercept resources loaded within the webview has been already discussed but I
feel we need a very simple implementation rather than creating a full-blown
messaging pattern which will just hurt (maintainability, complexity) in the long run.

Also, my Java is pretty rusty ;-)
@odino odino force-pushed the enable-callbacks-from-webview branch from 0d74c6f to 5bb43b9 Compare April 24, 2016 06:14
@facebook-github-bot
Copy link
Contributor

@odino updated the pull request.

@facebook-github-bot
Copy link
Contributor

@odino updated the pull request.

@stereodenis
Copy link
Contributor

@odino
Copy link
Contributor Author

odino commented Apr 24, 2016

@stereodenis that wont work for legacy implementations that are based on calling custom URLs :) even though I think it's a good solution I was aiming at taking care of the practice of using custom URLs since there's a lot of android + webview implementations that use that method.

@ghost
Copy link

ghost commented May 13, 2016

@mkonicek would you mind taking a look at this pull request? It's been a while since the last commit was reviewed.

@stereodenis
Copy link
Contributor

@odino will it be replacement for react-native-webview-bridge?

@odino
Copy link
Contributor Author

odino commented May 15, 2016

@stereodenis well, I would like to see a default way to implement some sort of communication between webviews and RN :)

@ghost ghost added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 12, 2016
@stereodenis
Copy link
Contributor

alinz/react-native-webview-bridge#109

@mkonicek
Copy link
Contributor

mkonicek commented Sep 8, 2016

Sorry for the delay on reviewing this - there's now a related proposal using postMessage: #9762

facebook-github-bot pushed a commit that referenced this pull request Oct 16, 2016
Summary:
JS API very similar to web workers and node's child process.

Work has been done by somebody else for the Android implementation over at #7020, so we'd need to have these in sync before anything gets merged.

I've made a prop `messagingEnabled` to be more explicit about creating globals—it might be sufficient to just check for an onMessage handler though.

![screen shot 2016-09-06 at 10 28 23](https://cloud.githubusercontent.com/assets/7275322/18268669/b1a12348-741c-11e6-91a1-ad39d5a8bc03.png)
Closes #9762

Differential Revision: D4008260

fbshipit-source-id: 84b1afafbc0ab1edc3dfbf1a8fb870218e171a4c
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 16, 2016
@lacker
Copy link
Contributor

lacker commented Oct 25, 2016

Since the postMessage change is accepted with #9762 I think this pull request has been superceded. I'm going to close this PR but please feel free to reopen if this is hitting a different use case or has an independent rationale.

@lacker lacker closed this Oct 25, 2016
atticoos pushed a commit to robinpowered/react-native that referenced this pull request Sep 21, 2017
Implement a postMessage function and an onMessage event for webviews …

Summary:
JS API very similar to web workers and node's child process.

Work has been done by somebody else for the Android implementation over at facebook#7020, so we'd need to have these in sync before anything gets merged.

I've made a prop `messagingEnabled` to be more explicit about creating globals—it might be sufficient to just check for an onMessage handler though.

![screen shot 2016-09-06 at 10 28 23](https://cloud.githubusercontent.com/assets/7275322/18268669/b1a12348-741c-11e6-91a1-ad39d5a8bc03.png)
Closes facebook#9762

Differential Revision: D4008260

fbshipit-source-id: 84b1afafbc0ab1edc3dfbf1a8fb870218e171a4c
eritter pushed a commit to eritter/react-native that referenced this pull request Sep 11, 2019
Summary:
JS API very similar to web workers and node's child process.

Work has been done by somebody else for the Android implementation over at facebook#7020, so we'd need to have these in sync before anything gets merged.

I've made a prop `messagingEnabled` to be more explicit about creating globals—it might be sufficient to just check for an onMessage handler though.

![screen shot 2016-09-06 at 10 28 23](https://cloud.githubusercontent.com/assets/7275322/18268669/b1a12348-741c-11e6-91a1-ad39d5a8bc03.png)
Closes facebook#9762

Differential Revision: D4008260

fbshipit-source-id: 84b1afafbc0ab1edc3dfbf1a8fb870218e171a4c
eritter pushed a commit to eritter/react-native that referenced this pull request Sep 11, 2019
Summary:
JS API very similar to web workers and node's child process.

Work has been done by somebody else for the Android implementation over at facebook#7020, so we'd need to have these in sync before anything gets merged.

I've made a prop `messagingEnabled` to be more explicit about creating globals—it might be sufficient to just check for an onMessage handler though.

![screen shot 2016-09-06 at 10 28 23](https://cloud.githubusercontent.com/assets/7275322/18268669/b1a12348-741c-11e6-91a1-ad39d5a8bc03.png)
Closes facebook#9762

Differential Revision: D4008260

fbshipit-source-id: 84b1afafbc0ab1edc3dfbf1a8fb870218e171a4c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants