SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
290 stars 92 forks source link

How we add headers in socketclusterclient #149

Closed sathiyaraj45 closed 1 year ago

sathiyaraj45 commented 1 year ago

In my java socketclusterclient send the header details with some custom params the same things I have to implement node socketclusterclient. How I archive this?

maarteNNNN commented 1 year ago

You should be able to send any data in JSON over the socket in case you want any meta data. E.g.:

{
  "customParams": { "any" : "thing" }
}

I recommend improving your question because it's vague and also include a more explicit case example which I can determine from what you want to achieve, perhaps some code.

sathiyaraj45 commented 1 year ago

const options = { hostname: 'localhost', port: 8000, agentName : 'test2' };

const headers = { 'Content-Type': 'application/json', 'X-Custom-Header': 'my-custom-value' };

const socket = socketCluster.create(options, { headers: headers });

I tried both way to send the agent details but we unable to receive the headers inform form server socket

{ 'sec-websocket-version': '13', 'sec-websocket-key': 'QuK/qshsusudhwedfs', connection: 'Upgrade', upgrade: 'websocket', 'sec-websocket-extensions': 'permessage-deflate; client_max_window_bits', host: 'localhost:8000' }

maarteNNNN commented 1 year ago

Headers are not being passed by the client. Why don't you emit an event e.g. socket.invoke('init', { headers: { any: "thing" } })?

sathiyaraj45 commented 1 year ago

I understand your response but I wonder how java socketcluster client send the header details(Existing code). also this is existing socketcluster server code and own other team. We unable to create an additional events.

MegaGM commented 1 year ago

Maybe it's possible to send custom headers via Java client, but in JavaScript client there is no support for custom headers, because custom headers during WebSocket connection are not supported by browsers.

https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api

this is existing socketcluster server code and own other team

If you are unable to change server side code, unfortunately, you have absolutely no way to pass additional information during connection phase from JavaScript client.

But if you could persuade the backend team to tweak the code, you could pass additional information using query params.

const socket = socketClusterClient.create({
  path: '/socketcluster/?myParam=42',
})

or

const socket = socketClusterClient.create({
  query: {
    myParam: 42,
  }
})

and they would catch it on the server side in MIDDLEWARE_HANDSHAKE

!async function () {
  agServer.setMiddleware(agServer.MIDDLEWARE_HANDSHAKE, async (middlewareStream) => {
    for await (const action of middlewareStream) {
      if (action.type === action.HANDSHAKE_WS) {
        console.info(action.request.url) // "/socketcluster/?myParam=42"
      }
      if (action.type === action.HANDSHAKE_SC) {
        console.info(action.socket.request.url) // "/socketcluster/?myParam=42"
      }
      action.allow()
    }
  })
}()