socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.32k stars 972 forks source link

Update README.md #716

Closed sup-fmarais closed 2 months ago

sup-fmarais commented 2 years ago

Hi, I am not sure where the repo is to edit/update the documentation here: https://socketio.github.io/socket.io-client-java/initialization.html

Could you please add a section around multiplexing. I have spent hours debugging an issue on our end where we did not init the sockets correctly for multiplexing. Could save other developers a lot of time. Here are the details.

Multiplexing

When using multiplexing please ensure you create your namespaces as follow

Set the options

IO.Options options = IO.Options.builder()
    .setMultiplex(true)

Incorrect way:

Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace

socket.connect();
productSocket.connect();

The first connect() will succeed to / and /product. The 2nd attempt will not set the socket.client.user object correctly. See screenshots below of attempt #1 and attempt #2 using the incorrect configuration as shown above.

Correct way:

Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = socket.io().socket("/product"); // the "product" namespace

socket.connect();
productSocket.connect();

This will reconnect correct every time, even if you reinit and reconnect the socket multiple times.

Failure:

failure

Success:

success
darrachequesne commented 2 years ago

Hi! That's weird, your first example should work properly:

Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace

socket.connect();
productSocket.connect();

System.out.println(socket.io() == productSocket.io()); // true

Regarding the socket.client.user object, are you using some middlewares on the server side?

sup-fmarais commented 2 years ago

Hi there, thanks for the response! On our server side we use expressjs and socket.io. Here is our package.json

{
    "name": "ws.superbalist.com",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "start": "node ./bin/www",
        "start-dev": "nodemon -L ./bin/www"
    },
    "dependencies": {
        "@google-cloud/pubsub": "2.19.4",
        "@superbalist/js-event-pubsub": "3.0.2",
        "@superbalist/js-pubsub-manager": "3.0.1",
        "axios": "0.21.1",
        "bluebird": "3.7.1",
        "body-parser": "1.19.0",
        "cookie-parser": "1.4.4",
        "debug": "4.1.1",
        "decimal.js": "10.2.0",
        "express": "4.17.1",
        "ip": "1.1.5",
        "jsonwebtoken": "8.5.1",
        "moment": "2.24.0",
        "morgan": "1.9.1",
        "newrelic": "6.1.0",
        "prom-client": "11.5.3",
        "pug": "2.0.4",
        "raven": "2.6.4",
        "redis": "2.8.0",
        "request": "2.88.0",
        "request-promise": "4.2.5",
        "serve-favicon": "2.5.0",

        # socket.io server
        "socket.io": "4.5.1",
        "socket.io-redis": "6.1.1",

        "uuid": "3.3.3",
        "winston": "3.3.3",
        "ws": "7.2.0"
    },
    "devDependencies": {
        "eslint": "7.32.0",
        "eslint-config-google": "0.14.0",
        "nodemon": "1.19.4",

        # I suspect this is for testing
        "socket.io-client": "4.1.3"
    }
}
darrachequesne commented 2 years ago

@sup-fmarais I'm afraid this won't be sufficient for me to be able to help you...

Could you please provide a complete example?

sup-fmarais commented 2 years ago

Sure I will try and set some time aside to do a client/server example matching our setup for you to test.

darrachequesne commented 2 months ago

I think this can be closed now. Please reopen if needed.