Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginning to understand web sockets and http requests (and STOMP)

I have recently joined a new project where I am being tasked with implementing streaming data using web sockets. The idea is a bunch of information is being exposed currently through HTTP requests (in a RESTful manner) that they want exposed through web sockets.

I have done a bunch of research in the past 48 hours about web sockets and STOMP and wanted to get some clarification on a few points:

  1. So for a client and a server to connect via a web socket rather than through an HTTP request/response, they first need to agree to set up a web socket connection between them. Is this done through HTTP GET with a unique header passed that says they are to use a web socket connection instead?

  2. Theoretically, say that there is a whole bunch of different data being exposed through some API to the browser. Imagine there is a whole bunch of different HTTP requests that can be made GET'S, POST'S, DELETE whatever. So to have certain pieces of all this information be streamed via a web socket, is it simply to change the current GET request for each resource to check to see if that special websocket header is there and then do something? Or is there something else that has to be done to expose certain pieces of data through web sockets. I just may of misunderstood the relationship of HTTP and sockets if you initialize a socket from a HTTP request.

I think these are my two main questions and I am sure the answers to these will point me in the right direction to continue learning more about the topic. I am trying to find any good sample code examples but I am trying to understand this well enough to implement within the week.

like image 867
user3037172 Avatar asked May 20 '15 02:05

user3037172


People also ask

What is STOMP in WebSockets?

STOMP, an acronym for Simple Text Oriented Messaging Protocol, is a simple HTTP-like protocol for interacting with any STOMP message broker. Any STOMP client can interact with the message broker and be interoperable among languages and platforms.

What is the difference between HTTP and WebSockets?

Unlike HTTP, where you have to constantly request updates, with websockets, updates are sent immediately when they are available. WebSockets keeps a single, persistent connection open while eliminating latency problems that arise with HTTP request/response-based methods.

Which is better WebSocket or HTTP?

All the frequently updated applications used WebSocket because it is faster than HTTP Connection. When we do not want to retain a connection for a particular amount of time or reuse the connection for transmitting data; An HTTP connection is slower than WebSockets.

What is STOMP API?

Simple (or Streaming) Text Oriented Message Protocol (STOMP), formerly known as TTMP, is a simple text-based protocol, designed for working with message-oriented middleware (MOM). It provides an interoperable wire format that allows STOMP clients to talk with any message broker supporting the protocol.


1 Answers

So for a client and a server to connect via a web socket rather than through an HTTP request/response, they first need to agree to set up a web socket connection between them. Is this done through HTTP GET with a unique header passed that says they are to use a web socket connection instead?

Yes, more or less. The headers are:

Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: <random string, base64 encoded>
Sec-WebSocket-Version: <version>

Typically version is 13 these days, although I believe 8 is also still in use.

If the server agrees to the websocket, it will return HTTP code 101 with the following headers:

Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: <base64 encoded hash based on Sec-WebSocket-Key>

Theoretically, say that there is a whole bunch of different data being exposed through some API to the browser. Imagine there is a whole bunch of different HTTP requests that can be made GET'S, POST'S, DELETE whatever. So to have certain pieces of all this information be streamed via a web socket, is it simply to change the current GET request for each resource to check to see if that special websocket header is there and then do something? Or is there something else that has to be done to expose certain pieces of data through web sockets. I just may of misunderstood the relationship of HTTP and sockets if you initialize a socket from a HTTP request.

It sounds like you understand this, but I will point out that you can only start a WebSocket with a GET.

From what you've described, it isn't clear that WebSockets are actually what you want. WebSockets are used for two-way communication between the server and client. If you just want to be able to stream from the server to the client, you'll have a much easier time with Server-Sent Events.

like image 66
Aaron Dufour Avatar answered Oct 24 '22 07:10

Aaron Dufour