My Ionic app can't access my local Node server from Android emulator - android-studio

I'm trying to run for the first time my React/Ionic app on an Android Emulator using Capacitor. The app should connect to my local Node server using Axios.
While my app started successfully on the emulator, all server requests failed with Msg: Error: Network Error
Here's the steps that I did:
I configured Axios to connect to http://10.0.2.2:3001 instead of localhost, but the requests still fail
I opened the url http://10.0.2.2:3001/api in the Android Emulator Chrome browser, and it succeeded in connecting to my server and returning a response
I tried adding to my capacitor.config.json file the following conf, but my app still fails
"server": {
"allowNavigation": ["10.0.2.2"]
}
Would you have other advices I should try to make my app connect to my local server? Thank you

After searching, I found the solution in another SO question :
The problem comes from the fact the my app uses http instead of https requests which Android forbids by default, starting with API level 28.
To allow http requests, I added android:usesCleartextTraffic="true" to my app's AndroidManifest.xml
Reference: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

if you use instances on cloud, modify host file, on linux the rute is : /etc/hosts
add this line:
localhost < ip-public-your-instance >

Related

Cant connect azureweb app to node server, resulting in net::ERR_CONNECTION_REFUSED

I am using node.js and react and have deployed to the azure app service,
the startup command is pm2 serve /home/site/wwwroot --no-daemon --spa which works
However when I visit the website and visit a page that needs to load images from a database or perform any http requests, the app can't connect to the node.js server and I get the error:
https://boobah.azurewebsites.net:8080/requests/therapists net::ERR_CONNECTION_TIMED_OUT
The call I make in my code to load the data to the page is this:
await axios.get('https://boobah.azurewebsites.net:8080/requests/therapists')
which works locally just by changing the domain to localhost and on port 3000.
Can anyone please help me for why this is not loading any data to the deployed azure app ?
Thanks you

NodeJS and React - LAN connection not working

Description
I have a Node server and React App running on my notebook Ubuntu 18.04.
Notebook ip is 10.0.0.101
I tried to connect my desktop to this server on the same network
Desktop ip is 10.0.0.100
Node is running on PORT=3333
React is running on PORT=3000
I can ping my notebook from my desktop but when I try to connect to node using Insomnia or to http://10.0.0.101:3000 from my desktop I cannot reach.
Errors
When try to connect to backend with Insomnia
Error: Couldn't connect to server
When try to connect to app on 10.0.0.101:3000
ERR_CONNECTION_TIMED_OUT
What I've done
I already changed the node listen and specified the port and the ip but it didn't work.
I tried to use my phone as router and connect from my phone to react on my notebook and also didn't work.
I've tried so many things since I can't remember them all.
I'm also creating a react-native app and when I try to connect to my mobile using Expo, it just works using tunnel, but cannot get data from the backend.
Please save me.
Thanks
After making many failed attempts, I realized that the problem was in my notebook.
I checked my firewall and it was disabled and still cannot get acess to my notebook on LAN. But finally I discovered what have to be done
Solution
You have to open the specific port for TCP traffic
Ubuntu:
sudo ufw allow <PORT-NUMBER>/tcp
or for Red Hat Enterprise
firewall-cmd --add-port <PORT-NUMER>/tcp
Reference: Examples of how to open firewall ports

socket.io-client-cpp does not connect to node app on Heroku via https

socket.io-client.cpp does not connect to node app on Heroku while Node JS socket.io-client does without any problem. Error is:
[2018-12-11 19:32:43] [connect] Successful connection
[2018-12-11 19:32:43] [error] handle_read_http_response error: websocketpp.transport:7 (End of File)
[2018-12-11 19:32:43] [info] Error getting remote endpoint: system:107
Changed URL from https to http://myapp.heroku.com - works now. Is it possible to connect via https as well? JS socket.io-client connects via secure connection without any problem.
Have built socket.io-client-cpp app with SIO_TLS in DEFINES (compiler flag: -DSIO_TLS) - connects via https fine now! This enables TLS support as mentioned here:
https://github.com/socketio/socket.io-client-cpp/pull/137

PeerJS Server 404 on Azure

I'm trying to deploy a PeerJS server on Azure. On my kudu console, running
node peerjs --port 9000
returns
Started PeerServer on ::, port: 9000, path: / (v. 0.2.8)
However, when I try to connect to the server from my client code, I get a 404. Going directly to appname.azurewebsites.net/peerjs/id in my browser also returns a 404.
I see inside their package.json file, they run
bin/peerjs --port ${PORT:=9000}
instead of just passing in 9000 directly; I assume this is an environment variable. However, trying to run this on Azure gives
Error: Error: listen EACCES ${PORT:=9000}
which I assume means Azure doesn't recognize ${PORT:=9000} as a valid port.
I know for a fact there's nothing wrong with my client side code because a) I copied it directly from PeerJS's website, and b) everything works correctly when I deployed PeerJS to Heroku. It's only not running on Azure.
Other things I've tried: I edited peerjs in the bin folder to use process.env.PORT instead of what's passed in via the command line, but that didn't work, giving the same EACCES error. When I tried to console.log(process.env.PORT), I got undefined. None of my Google searches have turned up any solutions, although this person (Custom PeerJs Server giving ERR_CONNECTION_TIMED_OUT) seems to have a similar error, not on Azure.
Azure App Service doesn't allow us to listen on a customer port. We need to use process.env.PORT instead. See Listen additional port Microsoft Azure Nodejs.
Azure App Service (on Windows platform) runs on Microsoft IIS. So we need to put the app files to its virtual directory (D:\home\site\wwwroot) and no longer need to manually run the app via the Kudu console.
In this case, you first need to install the library under app's root:
npm install peer
And then create a file named index.js or app.js with following content and put it to /wwwroot folder:
var PeerServer = require('peer').PeerServer;
var server = PeerServer({port: process.env.PORT, path: '/'});
As #Mikkel mentioned in a comment, PeerServer uses WebSocket protocol, so Web Sockets should be enabled in the Azure portal like this:
You also need to check out this post to add a web.config file for your app if it has not been created yet.
This will be a firewall problem... You will need to open port 9000 in your Azure settings panel.
From the machine itself, open up a browser to http://localhost:9000/ or http://localhost:9000/peerjs and you should see the standard Peerjs server JSON output.
Or if you only have command line, try curl http://localhost:9000/ or http://localhost:9000/peerjs

Difficulties connecting to socket.io server with Ionic

An overview of my setup:
A socket.io/node server is being hosted/ran on a computer in my local network.
An Ionic app is attempting to connect to the socket.io/node server to send/receive messages.
If I run the app in the browser with 'ionic serve', I am able to connect to the socket.io/node server successfully.
If I run the app in the emulator or on my device, I am only able to connect to the socket.io/node server if I add the -l (livereload) flag (ionic run android -l).
Originally, I hosted the node server on a heroku instance. I had no troubles connecting with this setup from browser, phone, or emulator. I had to switch to running the node server on a local computer so I have access to a local database.
Any ideas what is going on?
I fixed it by adding 'http://' to the beginning of the connection string.
Not sure why Ionic's livereload enabled me to connect without the 'http'

Resources