JavaScript WebSocket basics

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

WebSocket provides full-duplex communication with a server over a single TCP connection.

JavaScript Syntax

JavaScript
console.log(typeof WebSocket);
console.log("const ws = new WebSocket('wss://example.com/socket');");
Try it Editor

How It Works

  1. Constructor. Connect to a ws/wss URL.
  2. Events. open, message, close, error.

Examples

Example 1 — Example

Typical usage.

JavaScript
console.log(typeof WebSocket);
console.log("const ws = new WebSocket('wss://example.com/socket');");
Try it Editor

How It Works

  1. Constructor. Connect to a ws/wss URL.
  2. Events. open, message, close, error.

Example 2 — Support note

Check support.

JavaScript
console.log("WebSocket — verify browser/Node support");
Try it Editor

How It Works

  1. Support varies.

Frequently Asked Questions

WebSocket provides full-duplex communication with a server over a single TCP connection.
Check current support tables.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful