Code Monkey home page Code Monkey logo

Comments (22)

naderio avatar naderio commented on August 21, 2024 1

@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.

naderio avatar naderio commented on August 21, 2024

@Upd4ting do no pass 'polling' to transports options

from nativescript-socket.io.

Upd4ting avatar Upd4ting commented on August 21, 2024

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.

naderio avatar naderio commented on August 21, 2024

@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.

Upd4ting avatar Upd4ting commented on August 21, 2024

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.

Stanteq avatar Stanteq commented on August 21, 2024

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.

naderio avatar naderio commented on August 21, 2024

reopening, @Upd4ting @csi-stasik let's establish current state:

I've the following setup fully functional in prod:

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.

Upd4ting avatar Upd4ting commented on August 21, 2024

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.

Stanteq avatar Stanteq commented on August 21, 2024

@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.

naderio avatar naderio commented on August 21, 2024

@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.

Stanteq avatar Stanteq commented on August 21, 2024

@naderio Thank you, now the reason is clear.

from nativescript-socket.io.

Upd4ting avatar Upd4ting commented on August 21, 2024

@naderio I see, is it possible to have this option on android you think ?

from nativescript-socket.io.

naderio avatar naderio commented on August 21, 2024

@Upd4ting i'm inspecting the native java code, will see

from nativescript-socket.io.

Upd4ting avatar Upd4ting commented on August 21, 2024

Thanks!

from nativescript-socket.io.

naderio avatar naderio commented on August 21, 2024

@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.

Upd4ting avatar Upd4ting commented on August 21, 2024

Sweet!

Will this line const transports = Array.create(java.lang.String, 1); fail on IOS ?

from nativescript-socket.io.

Stanteq avatar Stanteq commented on August 21, 2024

@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.

Upd4ting avatar Upd4ting commented on August 21, 2024

@naderio Great! Thanks :)

from nativescript-socket.io.

Upd4ting avatar Upd4ting commented on August 21, 2024

@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.

naderio avatar naderio commented on August 21, 2024

@Upd4ting no idea

from nativescript-socket.io.

Stanteq avatar Stanteq commented on August 21, 2024

@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.

Upd4ting avatar Upd4ting commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.