Chad Johnson
Chad Johnson

Reputation: 21895

What is the purpose of a resource in a WebSocket URL?

Looking at the W3 spec on WebSockets, I see

var socket = new WebSocket('ws://game.example.com:12010/updates');
socket.onopen = function () {
  setInterval(function() {
    if (socket.bufferedAmount == 0)
      socket.send(getUpdateData());
  }, 50);
};

I understand that the socket services lives on port 12010 at game.example.com, but what is the purpose of the '/updates' resource in the URL? If the service lives at some port, what good will a resource do?

Upvotes: 13

Views: 5697

Answers (1)

Paul Batum
Paul Batum

Reputation: 8415

You can expose different logical WebSockets on the same port, using different URI's.

Lets take chat as an example. You could use the URI to determine the particular channel or chat room you want to join.

var socket = new WebSocket('ws://chat.example.com/games');
var socket = new WebSocket('ws://chat.example.com/movies');
var socket = new WebSocket('ws://chat.example.com/websockets');

You can also use query strings. Imagine a stock ticker:

var socket = new WebSocket('ws://www.example.com/ticker?code=MSFT');
var socket = new WebSocket('ws://www.example.com/ticker?code=GOOG');

Upvotes: 9

Related Questions