Questions tagged [websocket]

WebSocket is an API built on top of TCP sockets and a protocol for bi-directional, full-duplex communication between client and server without HTTP overhead.

WebSockets (or WebSocket) is an API and a protocol for bi-directional, full-duplex communication over TCP sockets. The WebSockets API was originally part of the HTML5 standard, but it has been split off into a separate W3C standard. The WebSockets protocol is an IETF standard described in RFC 6455.

The WebSockets API has full browser support in Chrome 14, Firefox 6, IE 10 (desktop and mobile), Opera 12.1 (desktop and mobile), Safari 6.0 (desktop and mobile), Android 4.4, Chrome Mobile, and Firefox Mobile. Some older browsers have partial support or can be supported using a Flash based fallback.

WebSockets supports both unencrypted and encrypted connections. Unencrypted connections use the "ws://" URL scheme and default to port 80. Encrypted connections use the "wss://" URL scheme and default to port 443. Encrypted connections use Transport Layer Security (TLS).

Simple WebSockets browser JavaScript example:

if ("WebSocket" in window) {
    var ws = new WebSocket("ws://echo.websocket.org/");
    ws.onopen = function() {
        console.log("WebSockets connection opened");
        ws.send("a test message");
    }
    ws.onmessage = function(e) {
        console.log("Got WebSockets message: " + e.data);
    }
    ws.onclose = function() {
        console.log("WebSockets connection closed");
    }
} else {
    // No native support
}

Useful Links

Books

27923 questions
1184
votes
4 answers

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

I have tried reading some articles, but I am not very clear on the concepts yet. Would someone like to take a shot at explaining to me what these technologies are: Long Polling Server-Sent Events Websockets Comet One thing that I came across every…
user1437328
  • 15,546
  • 9
  • 34
  • 44
1178
votes
7 answers

WebSockets vs. Server-Sent events/EventSource

Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies. What is the difference between them? When would you choose one over the other?
Mads Mobæk
  • 34,762
  • 20
  • 71
  • 78
683
votes
12 answers

Differences between socket.io and websockets

What are the differences between socket.io and websockets in node.js? Are they both server push technologies? The only differences I felt was, socket.io allowed me to send/emit messages by specifying an event name. In the case of socket.io a…
Vivek Mohan
  • 8,078
  • 8
  • 31
  • 49
452
votes
3 answers

Which websocket library to use with Node.js?

Currently there is a plethora of websocket libraries for node.js, the most popular seem to…
balupton
  • 47,113
  • 32
  • 131
  • 182
447
votes
6 answers

WebSockets protocol vs HTTP

There are many blogs and discussions about WebSocket and HTTP, and many developers and sites strongly advocate WebSockets, but I still can not understand why. For example (arguments of WebSocket lovers): HTML5 Web Sockets represents the next…
4esn0k
  • 9,789
  • 7
  • 33
  • 40
433
votes
10 answers

Does HTTP/2 make websockets obsolete?

I'm learning about HTTP/2 protocol. It's a binary protocol with small message frames. It allows stream multiplexing over single TCP connection. Conceptually it seems very similar to WebSockets. Are there plans to obsolete websockets and replace them…
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
371
votes
16 answers

HTTP headers in Websockets client API

Looks like it's easy to add custom HTTP headers to your websocket client with any HTTP header client which supports this, but I can't find how to do it with the web platform's WebSocket API. Anyone has a clue on how to achieve it? var ws = new…
Julien Genestoux
  • 31,046
  • 20
  • 66
  • 93
322
votes
4 answers

In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

I am building a small chat application for friends, but unsure about how to get information in a timely manner that is not as manual or as rudimentary as forcing a page refresh. Currently, I am implementing this using simple AJAX, but this has the…
somdow
  • 6,268
  • 10
  • 40
  • 58
302
votes
8 answers

WebRTC vs Websockets: If WebRTC can do Video, Audio, and Data, why do I need Websockets?

So I'm looking to build a chat app that will allow video, audio, and text. I spent some time researching into Websockets and WebRTC to decide which to use. Since there are plenty of video and audio apps with WebRTC, this sounds like a reasonable…
1ManStartup
  • 3,716
  • 4
  • 21
  • 26
277
votes
1 answer

What browsers support HTML5 WebSocket API?

I am going to develop an instant messaging application that runs in the browser. What browsers support the WebSocket API?
Sareuon
  • 2,787
  • 3
  • 16
  • 3
276
votes
6 answers

Difference between socket and websocket?

I'm building web app that needs to communicate with another application using socket connections. This is new territory for me, so want to be sure that sockets are different than websockets. It seems like they're only conceptually similar. Asking…
Jonathon
  • 2,793
  • 2
  • 15
  • 5
270
votes
12 answers

Debugging WebSocket in Google Chrome

Is there a way, or an extension, that lets me watch the "traffic" going through a WebSocket? For debugging purposes I'd like to see the client and server requests/responses.
mellowsoon
  • 22,273
  • 19
  • 57
  • 75
238
votes
12 answers

Send message to specific client with socket.io and node.js

I'm working with socket.io and node.js and until now it seems pretty good, but I don't know how to send a message from the server to an specific client, something like this: client.send(message, receiverSessionId) But neither the .send() nor the…
Rodolfo Palma
  • 2,831
  • 3
  • 16
  • 11
226
votes
7 answers

Why use AJAX when WebSockets is available?

I've been using WebSockets for a while now, I have chosen to create an Agile project management tool for my final year project at University utilizing Node server and WebSockets. I found using WebSockets provided a 624% increase in the number of…
Jack
  • 15,614
  • 19
  • 67
  • 92
214
votes
2 answers

Differences between TCP sockets and web sockets, one more time

Trying to understand as best as I can the differences between TCP socket and websocket, I've already found a lot of useful information within these questions: fundamental difference between websockets and pure TCP How to establish a TCP Socket…
pierroz
  • 7,653
  • 9
  • 48
  • 60
1
2 3
99 100