socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

unable to implement sticket sessions with aws load balancer

Open AndyGOBrien opened this issue 8 years ago • 3 comments

Does anyone know how to solve this?

ALB returns a cookie when connecting that is needed for the sticky sessions but i'm not sure how to implement that.

AndyGOBrien avatar May 11 '18 03:05 AndyGOBrien

Hey @AndyGOBrien did you manage to get any further with this or find a solution at all? Come across the same thing today

drofnnuD avatar Mar 27 '19 12:03 drofnnuD

@drofnnuD

My implementation with Kotlin and getting http response headers with rxjava2. it ain't pretty but it works. also, i'm not exactly sure how the stickiness works... looks like magic to me, but it does...

object Socket {

   val io: Socket = IO.socket(BuildConfig.SOCKET_URL)
   lateinit var cookie: String

    fun startSocketConnection(){
        setupHandlers()

        getHttpResponse()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe {
                    cookie = it.headers("Set-Cookie")[0]
                    Timber.d("Response")
                    io.connect()
                }
        Timber.d("startSocketConnection")

        io.io().on(Manager.EVENT_TRANSPORT, { args ->

            val transport = args[0] as Transport

            transport.on(Transport.EVENT_REQUEST_HEADERS, {
                val headers = it[0] as TreeMap<String, List<String>>
                headers["Cookie"] = Arrays.asList(cookie)
                Timber.d("Request Headers")
            })

            transport.on(Transport.EVENT_RESPONSE_HEADERS,{
                val headers = it[0] as TreeMap<String, List<String>>
                cookie = headers["set-cookie"]!![0]
                Timber.d("Transport Response")
            })
        })

    }

    private fun getHttpResponse(): Observable<Response> {
        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)
        val client = OkHttpClient()

        val request = Request.Builder()
                .url(BuildConfig.SOCKET_URL)
                .get()
                .build()

        return Observable.create { subscriber ->
            try {
                val response = client.newCall(request).execute()
                subscriber.onNext(response)
                subscriber.onCompleted()
            } catch (e: IOException) {
                e.printStackTrace()
                subscriber.onError(e)
            }
        }
    }
 
    //setup your "handlers" here
    private fun setupHandlers(){

        io.on(Socket.EVENT_CONNECT_ERROR, {
            Timber.d("Connection Failure: ")
        })
        io.on(Socket.EVENT_CONNECTING, {
            Timber.d("Connecting...")
        })
        io.on(Socket.EVENT_CONNECT, {
            Timber.d("Connected")
        })
        io.on(Socket.EVENT_PONG, {
            Timber.d("Pong: " + it[0] )
        })
        io.on(Socket.EVENT_PING, {
            Timber.d("Ping" )
        })

        //  ... etc .... more of your business specific handlers
    }

// ... your specific i/o stuff
}

AndyGOBrien avatar Mar 27 '19 14:03 AndyGOBrien

For future readers:

This was added in the documentation: https://socketio.github.io/socket.io-client-java/faq.html#How_to_use_with_AWS_Load_Balancing

Please reopen if needed.

darrachequesne avatar Jul 02 '22 05:07 darrachequesne