Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
Adding information about the client on the initial Websocket handshake

I'm currently creating a distributed chat application. Everything works fine, meaning it's possible to send messages between the clients and the server and have it broadcasted appropiately.

However, at the moment only the actual message is sent to the server. I would like to add information about the user sending the message aswell.

I could add this information whenever I send a new message, but I would prefer if I could add this information during the initial handshake and then save this information on the backend.

I've thought about sending some information in the URL, but as I only instantiate the websocket once, this does not seem like the way to go. Similarly, I thought about adding the information as the body of the request, but I read that having a body on a GET request is usually not recommended.

So my question is, am I trying to do something that I should not be going for? Should I just send information about the client on each new message that is sent to the server?

Currently, my client looks like this:

const socket = new WebSocket("ws://localhost:8080/ws");

const connect = (cb) => {
    console.log("Attempting Connection...")

    socket.onopen = () => {
        console.log("Successfully Connected");
    }

    socket.onmessage = (msg) => {
        console.log(msg)
        cb(msg);
    }

    socket.onclose = (event) => {
        console.log("Socket Closed Connection: ", event);
    }

    socket.onerror = (error) => {
        console.log("Socket Error: ", error);
    }
};

const sendMsg = (msg) => {
    console.log("Sending msg: ", msg);
    socket.send(msg);
}

And the initial connection on the backend is handled by the following:

func serveWs(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
    fmt.Println("WebSocket Endpoint Hit")

    conn, err := websocket.Upgrade(w, r)

    if err != nil {
        fmt.Fprintf(w, "%+V\n", err)
    }

    client := &websocket.Client{
        Name: "?????", // Obviously, I would like the actual name to be here.
        Conn: conn,
        Pool: pool,
    }

    pool.Register <- client

    client.Read()
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The normal thing to do here as selbie pointed out is to expect from the client a special first message through WebSocket. If that message is not recived or does not meet the requirements the server ends the WebSocket conn.

Using socket.onopen is very common for this task.

More on why you cant put headers: HTTP headers in Websockets client API

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda