Getting Socket.io to work in Android Webview - express

I am trying to use socket.io inside an Android Webview, inside a native Android app. The Android activity file has been updated with;
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/public/index.html");
My problem is loading the socket.io.js libraries from the index.html:
<script src="http://mydomain:port/socket.io/socket.io.js"></script>
The error I get during the rendering of the webview is:
XMLHttpRequest cannot load http://mydomain:port/socket.io... Origin null is not allowed by Access-Control-Allow-Origin.
I am using a node.js/express server to try and serve the libraries and thought I had configured this to accept cross-origin-resource-sharing (CORS) by setting
res.header('Access-Control-Allow-Origin', "*");
Now I am completely stuck! Can anybody recommend a way forward to get socket.io working in this configuration?
Cheers,
Kirbs

why not run service socket.io at activity, try my answer at this
singleton service socket.io
I using socket-io.java client library from github
Gottox

Related

Json-server how to set a custom URL for the api

I am working on an app and used json-server for the backend api. I published the app using github pages and everything seemed to be working fine on my laptop. But when I try to open the app on another device, it does not work. I realized this was because I am using localhost:3001 as the resource for my api, and this only exists on my laptop. I am now trying to change the resource from localhost:3001 to a custom url 'https://my-json-server.typicode.com/vbrambila2/1RM/movements'. The issue I'm running into now is that the POST, PATCH, and DELETE methods in my actions aren't updating the api anymore. Anyone know what I might be doing wrong?

GET request permissions error with React native expo and express server

I'm calling a GET request with axios in my React Native EXPO app but getting a NETWORK ERROR consoled in my terminal. I can make request to my Express server from my web browser but not my RN app.
I added this to my app.json file as I do to my React apps, but I'm not sure what the issue is. Any help is much appreciated. Thanks guys.
{
"expo": {
...
},
"proxy": "https://localhost:7000"
}
I figured it out! So I tried using axios.get("/") and axios.get("http://locahost:{PORT}/") and still had no luck but using axios.get("http://192.168.x.x:{PORT}/") worked. Looks like I need to use my local host IP adress to make API calls. I also removed the proxy script in my app.json

Release version of React Native Android app getting 'generic network error' when trying to access API over HTTPS

I am trying to run my React Native app (version 0.59.1) with an Android Device that is API 16 and does not have google play services. My problem is that when I build my app in release mode, calls to my API over HTTPS give me a "Network Error" with no further details. These calls are made using FETCH.
The API calls over HTTPS work fine when running in debug mode. I have also confirmed that just HTTP works when running in release mode.
I've seen several posts that suggest creating my own TLS/SSL connection factory but it seems that React Native should be already handling this with their OkHttpProvider: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java
I've also included pro-guard rules shown here: https://stackoverflow.com/a/38484643/3178433
I've also tried turning off pro-guard and that did not seem to make a difference.
Am I missing something? Is there a missing proguard rule? Is the OkHttpClientProvider provided by react not use like I am thinking it is?
Found the solution. I added this proguard rule and problem solved:
-keepclassmembers class * implements javax.net.ssl.SSLSocketFactory {
private final javax.net.ssl.SSLSocketFactory delegate;
}

disable http/2 requests in old react-native 0.38.1

We have upgraded our Nginx server and some requests from a mobile app build with react native 0.38.1 http requests stoped working. If we disable http/2 on nginx the requests works again. The problem is noticed only on android phones.
Is there a way to disable react native http2 calls?
We use RNFetchBlob.fetch(.... ) to execute http calls
the error we receive is
Error: Expected ':status' header not present

Impossible to use proxy

I'm trying to use proxy with my react-native app (build with CRNA and Expo).
So in my package.json file I put : "proxy": "192.168.1.27:8080" and then make simple get request with fetch with the/users` path and react call me network error (he don't see the proxy param, just /users when I'm debugging).
The fetch request work well if I put the full url.
In react (for website) I do the same thing and that's work to.
Thank's for your help.
In Reactjs proxies are mainly used to avoid cors issues. Since React Native connects to iOS & Android natively, there is no browser issue so you don't need a proxy.
The approach I use is to call http:127.0.0.1:nnnn/api/route (not localhost) in the React Native app when testing the backend, and the deployed url in production. So you can deploy your server separately and reach it directly in the app.