WebSocket API (WebSockets)

A WebSocket API é uma tecnologia que permite abrir uma sessão de comunicação interativa bidirecional entre o navegador do utilizador e um servidor. Com esta API, é possível enviar mensagens para um servidor e receber respostas orientadas por eventos sem ter de sondar o servidor para obter uma resposta.

Nota: Enquanto a conexão WebSocket é funcionalmente semelhante aos sockets de estilo Unix, não são relacionados.

Interfaces

WebSocket
A interface principal para fazer conexão a um servidor de WebSocket e depois enviar e receber dados na mesma conexão.
CloseEvent
O evento enviado pelo objeto WebSocket quando a conexão termina.
MessageEvent
O evento enviado pelo objeto WebSocket quando uma mensagem é recebida do servidor.

Guias

Ferramentas

  • HumbleNet: Uma biblioteca em rede multi-plataformas que funciona no navegador. Consiste num wrapper em C em torno de WebSockets e WebRTC que abstrai as diferenças entre navegadores, facilitando a criação de funcionalidades de rede multi-utilizador para jogos e outras aplicações.
  • µWebSockets: Implementação de servidor e cliente WebSocket altamente escalável para C++11 e Node.js.
  • ClusterWS:  Framework leve, rápida e poderosa para construir aplicações WebSocket escaláveis no Node.js.
  • CWS: Implementação rápida WebSocket de C++ para Node.js (uWebSockets v0.14 fork)
  • Socket.IO: Um protocolo de transferência de terceiros, baseado em long polling/WebSocket para Node.js.
  • SocketCluster: Uma pub/sub WebSocket framework para Node.js com foco na escalabilidade.
  • WebSocket-Node: Uma implementação da API de servidores WebSocket para Node.js.
  • Total.js: Uma framework de aplicações web para Node.js (Example: WebSocket chat)
  • Faye: Uma WebSocket (ligações bidireccionais) e EventSource (ligações unidireccionais) para Node.js Server e Client.
  • SignalR: SignalR usa WebSockets quando está disponível, e discretamente volta para outras técnicas e tecnologias quando não está, enquanto o seu código de aplicação permanece o mesmo.
  • Caddy: Um servidor web capaz de substituir comandos arbitrários (stdin/stdout) com uma web socket.
  • ws: uma biblioteca de cliente e servidor WebSocket popular para Node.js.
  • jsonrpc-bidirectional: RPC assíncrono que, numa única ligação, pode ter funções exportadas no servidor e, e ao mesmo tempo, no cliente (cliente pode chamar servidor, servidor também pode chamar cliente).
  • cowboy: Cowboy é um pequeno, rápido e moderno servidor HTTP para Erlang/OTP com suporte para WebSocket.

Especificações

Especificação Estado Comentário
HTML Living Standard
The definition of 'WebSocket API' in that specification.
Living Standard
WebSockets Candidate Recommendation
RFC 6455: The WebSocket Protocol IETF RFC

Compatibilidade

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
WebSocketChrome Full support 4Edge Full support 12Firefox Full support 11
Full support 11
No support 7 — 11
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
No support 4 — 6
Notes
Notes Message size limited to 16 MB (see bug 711205).
IE Full support 10Opera Full support 12.1Safari Full support 5WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 14
Notes
Full support 14
Notes
Notes See bug 695635.
No support 7 — 14
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
No support 4 — 6
Notes
Notes Message size limited to 16 MB (see bug 711205).
Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0
WebSocket() constructorChrome Full support YesEdge Full support ≤79Firefox Full support 7
Full support 7
No support 4 — 7
Notes
Notes Parameter protocols not supported.
IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 7Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
binaryTypeChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
bufferedAmountChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
closeChrome Full support 4Edge Full support 12Firefox Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
IE Full support 10Opera Full support 12.1Safari Full support 5WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0
close eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android Full support Yes
error eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android Full support Yes
extensionsChrome Full support YesEdge Full support 12Firefox Full support 8IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 8Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
message eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android Full support Yes
oncloseChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
onerrorChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
onmessageChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
onopenChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
open eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android Full support Yes
protocolChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Supports protocol as specified by RFC 6455Chrome Full support 16Edge Full support 12Firefox Full support 11IE Full support 10Opera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 14Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android Full support 1.0
readyStateChrome Full support 43Edge Full support 12Firefox Full support 19IE Full support 10Opera Full support 30Safari Full support 10WebView Android Full support 43Chrome Android Full support 43Firefox Android Full support 19Opera Android Full support 30Safari iOS Full support 10Samsung Internet Android Full support 4.0
sendChrome Full support 4Edge Full support 12Firefox Full support 18
Notes
Full support 18
Notes
Notes See bug 775368.
No support 11 — 18
Notes
Notes Only parameter of type ArrayBuffer and String supported.
No support 8 — 11
Notes
Notes Only parameter of type String supported.
No support 4 — 8
Notes
Notes Only parameter of type String supported. Returns boolean.
IE Full support 10Opera Full support 12.1Safari Full support 5WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 18
Notes
Full support 18
Notes
Notes See bug 775368.
No support 14 — 18
Notes
Notes Only parameter of type ArrayBuffer and String supported.
No support 8 — 14
Notes
Notes Only parameter of type String supported.
No support 4 — 8
Notes
Notes Only parameter of type String supported. Returns boolean.
Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0
urlChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Available in workersChrome Full support YesEdge Full support ≤18Firefox Full support 37IE ? Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 37Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

Ver também