Questions tagged [websockets]

WebSocket is an API and a protocol for bi-directional, full-duplex communication that is closely associated with HTML5 and implemented in recent versions of most web browsers.

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.

WebSockets has native implementations that are enabled by default in Chrome 4, Safari 5, and iOS 4.2. Firefox 4, and Opera 11 also have WebSockets built-in, but it was disabled by default until a fix in Firefox 6. Other browsers can support WebSockets 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

133 questions
58
votes
2 answers

Is performance the only reason not to use SignalR (websockets) entirely in lieu of a traditional REST API?

I have used SignalR to achieve real-time messaging functionality in several of my projects. It seems to work reliably and is very easy to learn to use. The temptation, at least for me, is to abandon developing a Web API service and use SignalR for…
tacos_tacos_tacos
  • 1,071
  • 1
  • 9
  • 16
27
votes
1 answer

RESTful HTTP and websocket in the same application?

If an application has already an opened WebSocket for live feeds, should I use it over AJAX for the other communications with the server? Because the connection is already opened, should we use it for requests that are Request/Response and not real…
Marc
  • 535
  • 2
  • 5
  • 11
24
votes
2 answers

How to architecture a realtime-heavy websockets-based web application?

In the process of developing a realtime Single Page Application, I have progressively adopted websockets to empower my users with up to date data. During this phase, I was sad to notice that I was destroying way too much of my app structure, and I…
Philippe Durix
  • 303
  • 1
  • 3
  • 7
11
votes
2 answers

Mixing REST and websocket in the same API

A bit of background first, I'm working on an application that manages a piece of physical equipment with motors, stuff like that. When I started on the project, there was an existing application, built with Labview and that was in a poor state. The…
Gimly
  • 211
  • 2
  • 5
11
votes
2 answers

Is masking really necessary when sending from Websocket client

The current Websocket RFC requires that websocket clients mask all data within frames when sending (but server is not required to). The reason the protocol was designed this way is to prevent frame data from being altered by malicious services…
JSON
  • 321
  • 2
  • 11
10
votes
2 answers

REST or a message queue in a multi-tier heterogeneous system?

I'm designing a REST API for a three-tier system like: Client application -> Front-end API cloud server -> user's home API server (Home). Home is a home device, and is supposed to maintain connection to Front-end via Websocket or a long poll (this…
9
votes
1 answer

Do I actually need a message broker or are websockets enough?

The website I am building has a real-time messaging component. The backend is built with Flask and I have integrated Flask-SocketIO to handle Websocket connections when users are on the messaging page. My current infrastructure is pretty simple.…
turnip
  • 1,657
  • 2
  • 15
  • 21
8
votes
1 answer

How long should Websockets maintain connection

I've created a rest generic service which additionally consume a websocket per resource my websocket code looks like so: initialize(hubSubRoute: string): void{ const accessToken = this.authManager.getRawAccessToken(); let hubUrl =…
johnny 5
  • 335
  • 3
  • 11
7
votes
2 answers

Is Kafka needed in a realtime chat application?

I'm developing a realtime chat application with an Angular frontend and Java backend. I've found a couple of examples that resemble what I am trying to achieve, such…
Babyburger
  • 211
  • 1
  • 3
  • 6
7
votes
5 answers

How do I balance 100 clients checking the same database table in a loop?

What I have This is a prototype. I have a pool of 100 clients connected to the server via websockets reporting things and awaiting for commands. The server polls the commands database table of type MEMORY in a loop using a query with WHERE…
6
votes
2 answers

Using websockets for server “broadcasting”?

I want to make it possible to edit shared todo lists in real time. There's already a REST interface, and it's possible to send updates to the server - add items, mark items as done, etc. For the client to get these updates immediately, I did some…
User
  • 341
  • 4
  • 10
5
votes
2 answers

Real time chat at scale

I have been looking at different solutions for large scale chatting solutions. I feel as if I understand 90 % of it but am turning to this forum to tie the knot. I imagine running a bunch of message servers behind a load balancer, keeping long lived…
Frankster
  • 159
  • 2
  • 10
5
votes
1 answer

Is there an equivalent to HTTP status codes for WebSockets messages?

I'm developing a WebSocket API and I was wondering if there was either a standard or best practice for responding to client messages. For example, if my API is expecting only stringified numbers but receives a word, what's the best way to respond? …
Justin Borromeo
  • 401
  • 1
  • 4
  • 6
5
votes
2 answers

What is the point of rooms in socket.io?

I was wondering what the purpose of rooms is. See: https://socket.io/docs/rooms-and-namespaces/ You can basically mimic join and leave with just socket.on and socket.off Take for example: (no room) client: socket.on('chat', (data) => { …
A. L
  • 217
  • 1
  • 4
  • 10
5
votes
1 answer

Does the issue of battery life for constant polling warrant the extra logic/time to implement a solution with websockets?

I am in the process of designing a chat application with cordova for android devices. I have been researching and have come to the conclusion that there are two ways to go about this that could work. Websockets. Using websockets the client…
nullReference
  • 295
  • 5
  • 11
1
2 3
8 9