What's New


  • Update Swift verson to 5.9.
  • Update dependencies.

WebSocket wrapper around URLSessionWebSocketTask

(macOS, iOS, iPadOS, tvOS, and watchOS)

A concrete implementation of a WebSocket client implemented by wrapping Apple's NWConnection.

The public interface of WebSocket is a simple struct whose public methods are exposed as closures. The reason for this design is to make it easy to inject fake WebSockets into your code for testing purposes.

The actual implementation is SystemWebSocket, but this type is not publicly accessible. Instead, you can access it via WebSocket.system(url:). SystemWebSocket tries its best to mirror the documented behavior of web browsers' WebSocket. Please report any deviations as bugs.

WebSocket exposes a simple API and makes heavy use of Swift Concurrency.


To use WebSocket, add a dependency to your Package.swift file:

let package = Package(
  dependencies: [
      url: "https://github.com/shareup/websocket-apple.git",
      from: "4.0.0"


// `WebSocket` starts connecting to the specified `URL` immediately.
let socket = WebSocket.system(url: url(49999))

// Wait for `WebSocket` to be ready to send and receive messages.
try await socket.open()

// Send a message to the server
try await socket.send(.text("hello"))

// Receive messages from the server
for await message in socket.messages {

try await socket.close()


  1. In your Terminal, navigate to the websocket-apple directory
  2. Run the tests using swift test


  • Swift Tools 5.9.0
View More Packages from this Author


Last updated: Fri May 03 2024 10:48:08 GMT-0900 (Hawaii-Aleutian Daylight Time)