staltz / react-native-node

Run a separate Node.js process behind a React Native app
MIT License
1.12k stars 33 forks source link

Running example code results in network error at RN fetch() #5

Open aschrijver opened 7 years ago

aschrijver commented 7 years ago

First of all: Thanks for this project!

I am checking out the /example code and when running as described in the README and without modifications I get following error:

TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (fetch.js:441)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
    at XMLHttpRequest.js:485
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:260)
    at MessageQueue.js:101
    at MessageQueue.__guard (MessageQueue.js:228)
    at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:100)

I will look into this further, but maybe there is something obvious I am overlooking..

(Versions: yarn 0.24.6, npm 4.2.0, ubuntu 16.04, (local) node: 7.10.1)

PS I will try again with downgraded node on ubuntu system

aschrijver commented 7 years ago

Downgrading first to 7.1.0, then to 6.9.1 did not solve the issue

staltz commented 7 years ago

Can you try making a release build? (would rule out the case where JS is running on your computer. it would run in the phone)

staltz commented 7 years ago

Oh, and are you running it in an emulator or device? (If device:) Is it 64bit or 32bit (e.g. which arm is it?)

aschrijver commented 7 years ago

I am running in the emulator with a Nexus_5X_API_26_x86(AVD) - 8.0.0

The release build has the same issue I think, but suppresses the error, so nothing happens when clicking link. (have also tried Nexus_4_API_23(AVD) - 6.0 with same result)

But I still have one adb error I can't get rid of (don't know if related to the problem, strange error as I only have one emulator up):

:app:installRelease
Installing APK 'app-release.apk' on 'Nexus_5X_API_26_x86(AVD) - 8.0.0' for app:release
Installed on 1 device.

BUILD SUCCESSFUL

Total time: 11.144 secs
Running adb -s emulator-5554 reverse tcp:8081 tcp:8081
adb server is out of date.  killing...
* daemon started successfully *
error: more than one device/emulator
Could not run adb reverse: Command failed: adb -s emulator-5554 reverse tcp:8081 tcp:8081
Starting the app on emulator-5554 (adb -s emulator-5554 shell am start -n com.example/com.example.MainActivity)...
Starting: Intent { cmp=com.example/.MainActivity }
staltz commented 7 years ago

If you have a device, experiment with that. With emulators, I've had issues in the past when it comes to networking.

aschrijver commented 7 years ago

Yes thx, I was about to do that, but just tested with node-on-android on an arm64 emulator. That works, but it has a bit different install process. Will now try real device. Just checking what proc it has (otherwise I need to boot an old phone).

Update: My own phone is 64-bit Samsung A3 (https://en.wikipedia.org/wiki/Samsung_Galaxy_A3_(2015)). Going to look into the drawers for an old phone :)

aschrijver commented 7 years ago

I just tried a real Samsung S3 Mini, but unfortunately it has the same release build issues as occurred with the emulator.

aschrijver commented 7 years ago

I though this may be the cause: https://github.com/facebook/react-native/issues/15100#issuecomment-317198976

So I downgraded to 0.45.1 but unfortunately with the same result. Afterwards for good measure I also tested with a Nexus_5X_API_24(AVD) - 7.0 without success.

Probably the node app is not responding. I cannot load the URL from a webview on the device as well.

aschrijver commented 7 years ago

FYI:

I've tried many different ways to get NodeJS running on Android and spent yesterday all-day compiling without success. I am investigating some viable-seeming approaches to use NodeJS in an android project. I have documented my findings on stackoverflow - Viable options to running NodeJS on Android (Aug 2017). You are mentioned there as well.

aschrijver commented 7 years ago

Hi @staltz

I got it working by refactoring to J2V8:

Steps:

Then RNNodeService.java like this:

(PS You don't need a V8Thread. A normal one will do)

For some reason I had to redefine RNNodeModule.start to have no parameters when called with no parameters from RN, otherwise I ended up with error NativeArgumentsParseException got 0 arguments expected 1. Will look into this later.

I now know much more on J2V8 now (see Viable options to running NodeJS on Android (Aug 2017)) and am convinced that both react-native-node and node-on-android would benefit by using this as a basis to build on further. It would not add any overhead in the way the project is working now, and gives some powerful options in e.g. communicating between RN and Node.

I will keep you posted on more findings.

aschrijver commented 7 years ago

One thing not working well yet in above setup is starting and stopping the Service in the case where the Node app is long-running express webserver (haven't tested with original react-native-node, may be an issue as well). This leads to crashes in the app.

Update I am going to refactor to a bound foreground sticky Service (running always independent of app), with NodeJS on its own thread.

erikdung commented 7 years ago

Any progress here?

aschrijver commented 7 years ago

This is current status: https://github.com/eclipsesource/J2V8/issues/332 May be solved when J2V8 is ready to build Node 8.4.0 (which is almost)

erikdung commented 6 years ago

No progress?

aschrijver commented 6 years ago

@erikdung i managed to get the whole react - node thing working with a modified version of this project that uses J2V8 instead

with the new docker build system in J2V8 this was quite easy to get working.. in fact i would advise @staltz to build this project on top of J2V8, not reinventing the wheel and join forces on making that core module better instead

having said that, on the whole node-on-android concept i found there where problems stopping and starting the service thread running node, leading to crashes of the app

unfortunately i do not have time now to investigate further, as i am working on innercircles, a tech initiative and movement for people who care :)

staltz commented 6 years ago

@aschrijver thanks for all this investigation, I think you have a very good point about J2V8, and I'll give it a try soon enough. I'll comment back on this thread later.

aschrijver commented 6 years ago

that would be cool! i'll jump back to node on android as soon as i have time again..

ps. in the J2V8 project we discussed some interesting ideas to grow the project, make it a solid base for any V8 on android.. the discussion is a bit silent now, but should've blow some more steam into that: https://github.com/eclipsesource/J2V8/issues/335

p30arena commented 6 years ago

@staltz This is what I get on a platform 23 x86 emulator:

[1]: syntax error: '☺t⌂544O☺☻♣4' unexpected

p30arena commented 6 years ago

The NodeJS Binary Included won't work because it is compiled for arm-v7 not x86

p30arena commented 6 years ago

I've created an issue on NodeBase repo, asking for compiling instructions

https://github.com/dna2github/NodeBase/issues/5

does anyone have knowledge on how to patch and compile nodejs for Android? _

aschrijver commented 6 years ago

@p30arena I wrote something here: https://stackoverflow.com/a/45649995/8295283 and kept track of compilation stuffz here: https://github.com/nodejs/node/issues/14783

hope this helps you a bit :)