HTTP/Websocket 'Handshake' Flow Help

Hi all,

I’d like to implement the following handshake method between a Go server and a JavaScript client:

  1. Client sends requested session ID to server
  2. Server returns assigned session ID to client (this may or may not be the same as the requested ID)
  3. Connection is upgraded to Websocket

The assigned session ID should be established as context for the WS session, rather than sent on top of the established WS.

An ideal flow may be:

  1. Client attaches session ID to the HTTP request as header or in query string
  2. Server decides on assigned ID and attaches it to the HTTP 1.1 101 Switching Protocols response as a header
  3. Client acknowledges and WS is established.

Key characteristics are that:

  1. The client can request an ID
  2. From the moment the WS is established, both the client and server are aware of the assigned session ID

I understand this approach cannot work due to header limitations with websockets, but maybe something like this:

  1. Client includes session ID to the HTTP request in query string (new Websocket("ws://localhost:3000/ws?session=123"))
  2. Server either sends the 101 response to upgrade the connection OR 302 redirects the client to a URL including the assigned session ID, which then follows with a 101.
  3. The client can introspect the assigned session ID with ws.url.

I tried hacking this together, but receive:

WebSocket connection to 'ws://localhost:3000/ws?session=123' failed: Error during WebSocket handshake: Unexpected response code: 302

I would appreciate any guidance on architecting a flow that achieves my desired characteristics.

Thanks!

Hi Chris.

just passing by, I would use/(get inspired with) gorilla sessions and
gorilla websocket. Will read more in detail your question though during the
day.

Hope it helps.

Have a nice week-end.

Patrick

Chris,

this article should help you too:

The approach here is to use the Golang “context” package.

Have a nice hacking day.

P

Hi there.

A practical guide with gorilla websockets, could help too:

P

Thanks for your help @pmjtoca!

I landed up implementing the “session negotiation handshake” on top of the WS instead of prior to the connection being upgraded. Upon connecting, the client requests a session (with ID) via a WS message and the server responds with assigned session ID which the client persists in memory.

It’s not exactly what I wanted, but does the job!

I’m just hacking together the functionality I’m looking for at the moment. I completely expect that this will need a serious rewrite to be (Go) semantically correct, but I want to create a prototype first to validate some assumptions. Maybe I could get your guidance with the rewrite :slight_smile:

Chris

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.