ZetCode

Dart WebSocket

last modified October 18, 2026

The WebSocket class in Dart provides full-duplex communication channels over a single TCP connection. It enables real-time data exchange between clients and servers.

WebSocket is ideal for applications requiring persistent connections like chat apps, live updates, and multiplayer games. The server-side implementation is part of Dart's dart:io library; browser applications use the WebSocket API from dart:html or package:web.

Basic Definition

WebSocket is a protocol providing full-duplex communication over a single TCP connection. It establishes a persistent connection between client and server.

Key features include event-driven communication, low latency, and efficient data framing. It's supported by all modern browsers and many server platforms.

Basic WebSocket Client

This example shows how to create a simple WebSocket client in Dart.

main.dart
import 'dart:io';

void main() async {
  try {
    var socket = await WebSocket.connect('ws://echo.websocket.org');
    print('Connected to WebSocket server');
    
    socket.add('Hello from Dart!');
    
    await for (var message in socket) {
      if (message == 'Hello from Dart!') {
        print('Received: $message');
        break;
      }
    }
    await socket.close();
  } catch (e) {
    print('Error: $e');
  }
}

We connect to a public WebSocket echo server, send a message, and listen for responses. The connection is closed after receiving the echo response.

$ dart main.dart
Connected to WebSocket server
Received: Hello from Dart!

WebSocket with Error Handling

This example demonstrates robust WebSocket usage with error handling.

main.dart
import 'dart:io';

void main() async {
  WebSocket? socket;
  
  try {
    var ws = await WebSocket.connect('ws://echo.websocket.org');
    socket = ws;
    ws.listen(
      (data) {
        if (data == 'Ping' || data == 'Ping 2') {
          print('Message: $data');
        }
      },
      onError: (err) => print('Error: $err'),
      onDone: () => print('Connection closed'),
    );
    
    ws.add('Ping');
    await Future.delayed(Duration(seconds: 1));
    ws.add('Ping 2');
    
    await Future.delayed(Duration(seconds: 1));
    await ws.close();
  } catch (e) {
    print('Connection error: $e');
    await socket?.close();
  }
}

We establish a connection with comprehensive error handling. The example shows multiple message exchanges and proper connection cleanup.

$ dart main.dart
Message: Ping
Message: Ping 2
Connection closed

WebSocket Server

This example demonstrates creating a simple WebSocket server in Dart.

server.dart
import 'dart:io';

void main() async {
  final server = await HttpServer.bind('localhost', 8080);
  print('WebSocket server listening on ws://localhost:8080');
  
  await for (var request in server) {
    if (WebSocketTransformer.isUpgradeRequest(request)) {
      var socket = await WebSocketTransformer.upgrade(request);
      handleConnection(socket);
    } else {
      request.response.statusCode = HttpStatus.forbidden;
      request.response.close();
    }
  }
}

void handleConnection(WebSocket socket) {
  print('Client connected');
  
  socket.listen(
    (message) {
      print('Received: $message');
      socket.add('Echo: $message');
    },
    onError: (error) => print('Error: $error'),
    onDone: () => print('Client disconnected'),
  );
}

We create a WebSocket server that echoes received messages back to clients. The server handles multiple concurrent connections and properly manages errors.

$ dart server.dart
WebSocket server listening on ws://localhost:8080
Client connected
Received: Hello
Client disconnected

Binary Data Transfer

This example shows how to send and receive binary data over WebSocket.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() async {
  var socket = await WebSocket.connect('ws://localhost:8080');
  
  // Send binary data
  var data = Uint8List.fromList([1, 2, 3, 4, 5]);
  socket.add(data);
  
  // Handle binary responses
  socket.listen((message) {
    if (message is Uint8List) {
      print('Received binary: ${message.toList()}');
    } else {
      print('Received text: $message');
    }
    socket.close();
  });
}

We demonstrate sending binary data as Uint8List and handling different message types. WebSocket supports both text and binary data transmission.

$ dart main.dart
Received binary: [1, 2, 3, 4, 5]

WebSocket with JSON

This example shows WebSocket communication using JSON for structured data.

main.dart
import 'dart:io';
import 'dart:convert';

void main() async {
  var socket = await WebSocket.connect('ws://localhost:8080');
  
  // Send JSON data
  var message = {'type': 'greeting', 'content': 'Hello Server'};
  socket.add(jsonEncode(message));
  
  // Handle JSON responses
  socket.listen((data) {
    try {
      var response = jsonDecode(data);
      print('Received JSON: ${response['type']} - ${response['content']}');
    } catch (e) {
      print('Error parsing JSON: $e');
    }
    socket.close();
  });
}

We use JSON for structured communication between client and server. The example shows encoding and decoding of complex data structures.

$ dart main.dart
Received JSON: response - Hello Client

Best Practices

Source

Dart WebSocket Documentation

This tutorial covered Dart's WebSocket class with practical examples showing client and server implementations, binary data transfer, and JSON messaging.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Dart tutorials.