JavaScript WebSockets

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

WebSocket is protocol, which enables two-way communication between a client and server:

The goal WebSocket is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections. (RFC 6455)

WebSocket works over HTTP protocol.

Syntax

  • new WebSocket(url)
  • ws.binaryType /* delivery type of received message: "arraybuffer" or "blob" */
  • ws.close()
  • ws.send(data)
  • ws.onmessage = function(message) { /* ... */ }
  • ws.onopen = function() { /* ... */ }
  • ws.onerror = function() { /* ... */ }
  • ws.onclose = function() { /* ... */ }

Parameters

ParameterDetails
urlThe server url supporting this web socket connection.
dataThe content to send to the host.
messageThe message received from the host.


Got any JavaScript Question?