Comments (22)
@Upd4ting you're right
const platformModule = require("tns-core-modules/platform");
function connect() {
let transports = null;
if (platformModule.isAndroid) {
transports = Array.create(java.lang.String, 1);
transports[0] = 'websocket';
}
socket.io = SocketIO.connect(PARAM.ENDPOINT, {
android: {
transports,
},
ios: {
forceWebsockets: true,
},
query: {
token: STATE.token,
},
});
};
from nativescript-socket.io.
@Upd4ting do no pass 'polling'
to transports
options
from nativescript-socket.io.
That's not a solution I already tried and first request always try doing polling but as my socket io is clusterised, the polling handshake won't work and trigger a transport error
from nativescript-socket.io.
@Upd4ting the following works for a similar use case as yours in production:
const socketio = SocketIO.connect(`${PARAM.ENDPOINT}/`, {
android: {
forceWebsockets: true,
},
ios: {
forceWebsockets: true,
},
query: {
token: STATE.token,
},
});
from nativescript-socket.io.
let token = randomString(15); socketio = SocketIO.connect('http://192.168.0.7:1337/app', { query: { token: token }, android: { forceWebsockets: true, }, ios: { forceWebsockets: true, } });
Tried some test in local and that's not working. Server side logs:
engine handling "POST" http request "/socket.io/?EIO=3&token=RJQTf8lLJA14xSZ&sid=BzdR8kN_NaAEcOjrAABX&transport=polling"
As you see that's still trying polling transport and not websocket :/
from nativescript-socket.io.
Why this post was closed without any working solution?
I have the same problem. Even I force the client to use only websocket, on clustered server I get always:
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&token=x"
engine setting new request for existing client
socket.io:client client close with reason transport error
socket.io:socket closing socket - reason transport error
from nativescript-socket.io.
reopening, @Upd4ting @csi-stasik let's establish current state:
I've the following setup fully functional in prod:
- multiple instances
- using socket.io-redis (https://socket.io/docs/using-multiple-nodes/#Passing-events-between-nodes)
- nginx load balancing (https://socket.io/docs/using-multiple-nodes/#NginX-configuration)
- client connects with snippet provided above
are you using a similar setup or not? and what versions are using of socket.io
in the backend and nativescript-socket.io
? android or ios or both?
from nativescript-socket.io.
Hello @naderio,
Thanks for trying to help!
You don't have the problem because you're using nginx load balancing that use ip hash system. IP hash ensure that a client will always be redirected to the same cluster and so polling handshake will success.
I'm using pm2 clustering with built-in load balancing and that's not using ip-hash.
That's why i'm trying to tell socket io to not event try to connect with polling and use websocket directly. But it seems that socket io keeps trying polling as first transport solution :/
from nativescript-socket.io.
@naderio, Thanks for replay
I have only dev environment for the moment, I'm using:
- Multiple workers/instances with pm2-dev in cluster mode (with single instance it's working fine)
- using socket.io-redis (but I'm not arrive to using it because after connection its disconnect, I'm not send/receive any other messages yet)
- My client options:
const options: SocketIO.SocketOptions = {
android: {
transports: ["websocket"],
// forceWebsockets: true -> I tried also with this.
}
query: {
token: "my_token"
}
};
const io = SocketIO.connect(environment.socketUrl, options);
Result:
Its called the connection event but after I get
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&token=x"
engine setting new request for existing client
socket.io:client client close with reason transport error
socket.io:socket closing socket - reason transport error
from nativescript-socket.io.
@Upd4ting I had the same setup which didn't work so I've switched load balancing to nginx instead of pm2, that's why it works for me even with polling
my setup have socket io listen to separate port per instance (making use of pm2' instance number/id process.env.NODE_APP_INSTANCE
) and nginx load balancing on those ports as in the provided snippet
@csi-stasik @Upd4ting for there is no forceWebsockets
for android => transports: ['websocket']
I'm looking into this ...
from nativescript-socket.io.
@naderio Thank you, now the reason is clear.
from nativescript-socket.io.
@naderio I see, is it possible to have this option on android you think ?
from nativescript-socket.io.
@Upd4ting i'm inspecting the native java code, will see
from nativescript-socket.io.
Thanks!
from nativescript-socket.io.
@Upd4ting @csi-stasik i've managed to make it work but it is not a clean code, maybe I add an option to do this, anyhow following works on websocket:
function connect() {
const transports = Array.create(java.lang.String, 1);
transports[0] = 'websocket';
socket.io = SocketIO.connect(`PARAM.ENDPOINT, {
android: {
transports,
},
ios: {
forceWebsockets: true,
},
query: {
token: STATE.token,
},
});
};
The issue was array conversion from JavaScript to Java
from nativescript-socket.io.
Sweet!
Will this line const transports = Array.create(java.lang.String, 1); fail on IOS ?
from nativescript-socket.io.
@Upd4ting, Great workaround, now it forcing the websockets only (in future version it will be nice to include also this fix). Thanks anyway.
But after your previous replay I like more the idea of nginx load balancing instead of pm2 (buildin load balancing) and polling sacrifice in clustered server.
from nativescript-socket.io.
@naderio Great! Thanks :)
from nativescript-socket.io.
@naderio Since i'm using this I got a crash on Huawei p20
com.tns.NativeScriptException: Calling js method instantiateItem failed
Error: java.lang.NoSuchMethodError: no non-static method "Landroid/support/v4/app/FragmentManagerImpl;.findFragmentByTag(Ljava/lang/String;Lio/socket/client/IO$Options;)Lio/socket/client/Socket;"
Do you know what that could be ?
from nativescript-socket.io.
@Upd4ting no idea
from nativescript-socket.io.
@Upd4ting, On other devices work? Can you share the code of socket initialization, I have no problems on Android simulator and LG G6 (I adapted the @Upd4ting's solution for typescript)
from nativescript-socket.io.
I used @naderio one, but that's doing that error only on some device and not every time I think
from nativescript-socket.io.
Related Issues (20)
- Reconnection
- need upgrade to {N} V4 ? HOT 2
- Can I use this plugin in NativeScript Angular? HOT 1
- IOS `init` is depricated HOT 8
- strange behavior can't build nativescript-socket.io HOT 3
- iOS Swift 4.0 Build Failed HOT 1
- Plugin is not compatible with xcode 10.2 HOT 4
- Flavor 'nativescript-socket.io' has unknown dimension 'nativescriptsocketio' HOT 1
- Deprecated? HOT 1
- use with feathersjs HOT 1
- Not working on android :( HOT 23
- tns plugin add nativescript-socket.io is not working with 0.11.0 HOT 1
- IOS Build failed
- Event subscription never fires iOS
- Disconnect issue
- Cant connect to server without path HOT 1
- Cant connect to nodejs socket v4 with io.socket:socket.io-client v1 HOT 3
- Issue with include.gradle and keyword "compile" on NS 8 HOT 2
- Fails to build
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nativescript-socket.io.