11

Alright so I just tried out the new WebSocket class in HTML 5, and was pretty excited they exist; however, I fail to see how they are much more rewarding than AJAX seeing as how they still initiate an HTTP call and are not like conventional sockets. That's why I'm asking here.

Is there a way with HTML 5's WebSocket class to connect to a listening socket without sending HTTP data? Currently with TCP/IP builder it's showing all this header crap that I don't want (since I want to connect to POP3/IMAP servers without things like Flash bridges or Comet).

Possible?

Output from connection:

Listening for connections...Connected
GET / HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:666
Origin: null
Sec-WebSocket-Key1: 2 987_390VNw60yi9
Sec-WebSocket-Key2: ~196  Y p  5    P67 428  ?
1
  • Just an update; W3C appears to be spec'ing a pure TCP/UDP socket API, though I'm sure it'll be several years before we see any sort of functionality for it. Sep 18, 2014 at 17:11

1 Answer 1

5

No. Once the connection is established you have a true socket. But you're right that it requires special server support. So it won't let you connect to an unmodified POP or IMAP server.

They chose that design (the Upgrade mechanism) so you could easily have a server that listened for WebSocket connections as well as true HTTP requests.

There is still a big distinction from AJAX and COMET. You can use WebSockets to have true full-duplex communication between server and client. Previous browser APIs haven't provided that, forcing people to use various work-arounds (such as repeated AJAX requests, COMET's forever frame, and others).

7
  • 1
    So it's pretty much AJAX that doesn't close? Kind of like a forever-frame combined with AJAX. Nifty, but not so nifty at the same time. Thank you for the information! Jan 4, 2011 at 15:24
  • it's not quite a pure TCP socket after the handshake because there is additional framing for each frame. @Di-0xide, it's really more like a socket with a HTTP-like handshake (and some minimal framing). The handshake makes it easier to add WebSockets support to web servers and proxies, but that's all. It's not an HTTP request, it's a WebSockets handshake that just looks like HTTP.
    – kanaka
    Jan 5, 2011 at 23:35
  • @kanaka, I didn't mean to imply it was a TCP socket. There are many other kinds (UDP, raw, domain, etc.). WebSocket is yet another. Jan 6, 2011 at 8:29
  • 2
    technically a WebSocket connection uses TCP sockets :-). But it's not a pure/raw TCP socket. TCP, UDP, etc are OSI layer 4 (transport). WebSockets are more in OSI layer 5 and 6. In theory you could have WebSocket connections over UDP (or domain socket, etc) but the current WebSockets definition is layered on TCP.
    – kanaka
    Jan 6, 2011 at 22:25
  • @kanaka, I didn't say it was a raw TCP socket, just that it was a socket (implemented over TCP). Jan 6, 2011 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.