VladiC4T
VladiC4T

Reputation: 236

What's the URI path component in websocket used for?

The client initiates a websocket handshake and can specify a custom URI path to use.

    GET **/chat** HTTP/1.1
    Host: server.example.com
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
    Origin: http://example.com
    Sec-WebSocket-Protocol: chat, superchat
    Sec-WebSocket-Version: 13

The websocket uri takes this form: ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]

If I deploy a websocket server listening on ws://localhost:8080/ and a client connects there with the following uri ws://localhost:8080/chat, what's the difference? Why is a path component even necessary? It's there just for load balancing? Or are these "endpoints" isolated?

The following statement in the RFC is quite confusing to me:

The "Request-URI" of the GET method [RFC2616] is used to identify the endpoint of the WebSocket connection, both to allow multiple domains to be served from one IP address and to allow multiple WebSocket endpoints to be served by a single server. (HERE)

Upvotes: 4

Views: 5789

Answers (1)

ottomeister
ottomeister

Reputation: 5828

If your websocket service always treats every connection identically then there's no need to require a path string, or a query string, in the URI. (Well, technically you do have to have a path string, but it can be just the single character '/' every time.)

However, if your websocket service wants to be able to provide different content to different clients, then using a path string in the URI can be a convenient way for the clients to indicate what kind of content they're interested in. Perhaps your chat service offers chat rooms for several different topics, and in that case you might decide to have the client use the path string to indicate the desired topic. Something like ws://localhost:8080/sport, ws://localhost:8080/politics or ws://localhost:8080/cooking, or you could take it further and have ws://localhost:8080/sport/football, ws://localhost:8080/sport/golf and ws://localhost:8080/sport/tennis, and so on.

The "Request-URI" of the GET method [RFC2616] is used to identify the endpoint of the WebSocket connection, both to allow multiple domains to be served from one IP address ...

This is just ordinary virtual hosting. A webserver running at a single IP address can support multiple websites, based on the host part of the URI.

... and to allow multiple WebSocket endpoints to be served by a single server.

Each of the path examples I gave identifies a different, independent endpoint, all served by your one websocket server.

In this particular example the websocket endpoint is some sort of chat room, but more generally a websocket endpoint is associated with some kind of data feed, often a one-way feed (a websocket might offer a stream of current stock prices for some collection of stocks specified by a URI query string) but potentially a bidirectional feed and potentially an interactive birdirectional feed (like your chat room).

Upvotes: 4

Related Questions