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
135
votes
7 answers

Getting the reason why websockets closed with close code 1006

I would like to get the reason websockets closed, so I can show the right message to the user. I have sok.onerror=function (evt) {//since there is an error, sockets will close so... sok.onclose=function(e){ …
slevin
  • 4,166
  • 20
  • 69
  • 129
134
votes
2 answers

What is the difference between grpc and websocket? Which one is more suitable for bidirectional streaming connection?

I want to develop a client-server application in a bi-directional streaming manner. what is more suitable technology for this, grpc or websocket?
prashant sindhu
  • 1,769
  • 2
  • 15
  • 25
133
votes
3 answers

Loadbalancing web sockets

I have a server which supports web sockets. Browsers connect to my site and each one opens a web socket to www.mydomain.example. That way, my social network app can push messages to the clients. Traditionally, using just HTTP requests, I would scale…
John Smith
  • 3,037
  • 5
  • 29
  • 31
132
votes
14 answers

WebSockets and Apache proxy: how to configure mod_proxy_wstunnel?

I have : Apache 2.4 on port 80 of my server, with mod_proxy and mod_proxy_wstunnel enabled Node.js + socket.io on port 3001 of the same server Accessing example.com (with port 80) redirects to 2. thanks to this method with the following Apache…
Basj
  • 41,386
  • 99
  • 383
  • 673
127
votes
6 answers

javax.websocket client simple example

Can someone please provide me very simple example of websocket client using javax.websocket? I want to connect to websocket (ws://socket.example.com:1234), send message (add channel) and listen to messages. All messages (sent & listened) are in JSON…
Martin Ille
  • 6,747
  • 9
  • 44
  • 63
127
votes
3 answers

Differences between webhook and websocket?

I've always wanted to do a real-time chat. I've done that years ago in PHP+Ajax+Mysql and broke my server. Then I tried with Flash+ a text file. I gave up and haven't tried in 10 years. But recently I heard about webhooks and websockets. And they…
David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
127
votes
5 answers

How to create websockets server in PHP

I am looking for a simple code to create a WebSocket server. I found phpwebsockets but it is outdated now and doesn't support the newest protocol. I tried updating it myself but it doesn't seem to work. #!/php -q php -q server.php …
Dharman
  • 30,962
  • 25
  • 85
  • 135
120
votes
9 answers

How to construct a WebSocket URI relative to the page URI?

I want to construct a WebSocket URI relative to the page URI at the browser side. Say, in my case convert HTTP URIs…
neuront
  • 9,312
  • 5
  • 42
  • 71
118
votes
2 answers

Differences between websockets and long polling for turn based game server

I am writing a server for an iOS game. The game is turn-based and the only time the server needs to push information to the client is to notify of the opponent's move. I am curious if anyone could comment on the performance and ease of…
acidic
  • 1,497
  • 2
  • 19
  • 23
118
votes
5 answers

Is there a WebSocket client implemented for Python?

I found this project: http://code.google.com/p/standalonewebsocketserver/ for a WebSocket server, but I need to implement a WebSocket client in python, more exactly I need to receive some commands from XMPP in my WebSocket server.
diegueus9
  • 29,351
  • 16
  • 62
  • 74
114
votes
1 answer

Xmpp Vs Websocket

I'm about to develop a website that has near real time chat. I know that it can be implemented using xmpp or websocket protocols. I know also that the xmpp protocol has been developed in 1999 , and I guess it should be mature nowadays .On the other…
Khafaga
  • 1,537
  • 4
  • 15
  • 24
113
votes
13 answers

webSocketServer node.js how to differentiate clients

I am trying to use sockets with node.js, I succeded but I don't know how to differentiate clients in my code. The part concerning sockets is this: var WebSocketServer = require('ws').Server, wss = new WebSocketServer({port:…
Ajouve
  • 9,735
  • 26
  • 90
  • 137
112
votes
9 answers

Websocket API to replace REST API?

I have an application whose primary function works in real time, through websockets or long polling. However, most of the site is written in a RESTful fashion, which is nice for application s and other clients in the future. However, I'm thinking…
Harry
  • 52,711
  • 71
  • 177
  • 261
109
votes
5 answers

WebSockets ping/pong, why not TCP keepalive?

WebSockets have the option of sending pings to the other end, where the other end is supposed to respond with a pong. Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame. It…
Thomas
  • 174,939
  • 50
  • 355
  • 478
108
votes
9 answers

Reconnection of Client when server reboots in WebSocket

I am using web socket using PHP5 and the Chrome browser as client. I have taken the code from the site http://code.google.com/p/phpwebsocket/. I run the server, and the client is also connected. I can chat as well. Now when I restart the server (by…
siddhusingh
  • 1,832
  • 4
  • 25
  • 30