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
211
votes
4 answers

What is the fundamental difference between WebSockets and pure TCP?

I've read about WebSockets and I wonder why browser couldn't simply open trivial TCP connection and communicate with server like any other desktop application. And why this communication is possible via websockets?
xap4o
  • 2,776
  • 2
  • 19
  • 13
209
votes
3 answers

Good beginners tutorial to socket.io?

I am very new to the world of webdevelopment and jumped into the bandwagon because I find the concept of HTML5 very interesting. I am fairly confident on working with canvas and would now like to move over to websockets part of it. I have come to…
Shouvik
  • 11,350
  • 16
  • 58
  • 89
188
votes
13 answers

node.js, socket.io with SSL

I'm trying to get socket.io running with my SSL certificate however, it will not connect. I based my code off the chat example: var https = require('https'); var fs = require('fs'); /** * Bootstrap app. */ var sys =…
Beyond
  • 2,050
  • 2
  • 13
  • 12
186
votes
6 answers

Maximum concurrent Socket.IO connections

This question has been asked previously but not recently and not with a clear answer. Using Socket.io, is there a maximum number of concurrent connections that one can maintain before you need to add another server? Does anyone know of any active…
Andrew
  • 14,204
  • 15
  • 60
  • 104
174
votes
2 answers

How do you inspect websocket traffic with Chrome Developer Tools?

I'm trying to inspect websocket traffic using Chrome Developer Tools. From my research it seems you should be able to see it using the network tab - and it even has a filter for 'ws'. However I've been inspecting sites that I know are using…
Joel Duckworth
  • 5,455
  • 3
  • 20
  • 21
174
votes
2 answers

How many system resources will be held for keeping 1,000,000 websocket open?

Websocket is good, but would it be able to handle 1,000,000 concurrent connections? How many system resources will be needed for keeping 1,000,000 websocket open? Thanks!
k.k. lou
  • 1,805
  • 2
  • 13
  • 16
171
votes
5 answers

Do HTML WebSockets maintain an open connection for each client? Does this scale?

I am curious if anyone has any information about the scalability of HTML WebSockets. For everything I've read it appears that every client will maintain an open line of communication with the server. I'm just wondering how that scales and how many…
rmontgomery429
  • 14,660
  • 17
  • 61
  • 66
170
votes
11 answers

WebSocket: How to automatically reconnect after it dies

var ws = new WebSocket('ws://localhost:8080'); ws.onopen = function () { ws.send(JSON.stringify({ .... some message the I must send when I connect .... })); }; ws.onmessage = function (e) { console.log('Got a message') …
samol
  • 18,950
  • 32
  • 88
  • 127
170
votes
4 answers

Sending websocket ping/pong frame from browser

I keep reading about ping/pong messages in websockets to keep the connection alive, but I'm not sure what they are. Is it a distinct frame type? (I don't see any methods on a javascript WebSocket object in chrome related to ping-pong). Or is it…
danny
  • 10,103
  • 10
  • 50
  • 57
169
votes
5 answers

What is the difference between WebSocket and STOMP protocols?

What are the major differences between WebSocket and STOMP protocols?
LancerSung
  • 1,711
  • 2
  • 9
  • 3
160
votes
6 answers

How can I get the sha1 hash of a string in node.js?

I'm trying to create a websocket server written in node.js To get the server to work I need to get the SHA1 hash of a string. What I have to do is explained in Section 5.2.2 page 35 of the docs. NOTE: As an example, if the value of the…
Eric
  • 2,886
  • 3
  • 20
  • 19
152
votes
3 answers

Which WebSocket library to use in Android app?

I want to add a Service to my Android app which runs in the background holding a WebSocket connection (possibly over several hours or even days) and regularly sends some data to a server. Now there seems to be a bunch of WebSocket libraries for…
x-ray
  • 3,279
  • 5
  • 24
  • 37
152
votes
4 answers

My Understanding of HTTP Polling, Long Polling, HTTP Streaming and WebSockets

I have read many posts on SO and the web regarding the keywords in my question title and learned a lot from them. Some of the questions I read are related to specific implementation challenges while others focus on general concepts. I just want to…
Software Guy
  • 3,190
  • 4
  • 21
  • 21
143
votes
7 answers

Closing WebSocket correctly (HTML5, Javascript)

I am playing around with HTML5 WebSockets. I was wondering, how do I close the connection gracefully? Like, what happens if user refreshes the page, or just closes the browser? There is a weird behavior when a user just refresh the page without…
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
137
votes
4 answers

WebSocket with SSL

Is it possible to have WebSockets with HTTPS? When switching to HTTPS, my WebSocket returns a security error and works perfectly with regular HTTP. Below, a snippet; socket = new WebSocket("ws://my_www:1235");
Eric
  • 9,870
  • 14
  • 66
  • 102