Skip to content

Getting Started with WebSocket

WebSocket (vapor/websocket-kit) is a non-blocking, event-driven WebSocket library built on SwiftNIO. It makes working with SwiftNIO's WebSocket handlers easy and provides integration with HTTP clients and servers. Creating a WebSocket echo server takes just a few lines of code.

Tip

If you use Vapor, most of WebSocket's APIs will be wrapped by more convenient methods.

Vapor

This package is included with Vapor and exported by default. You will have access to all WebSocket APIs when you import Vapor.

import Vapor

Standalone

The WebSocket package is lightweight, pure Swift, and only depends on SwiftNIO. This means it can be used as a WebSocket framework any Swift project—even one not using Vapor.

To include it in your package, add the following to your Package.swift file.

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        ...
        .package(url: "https://github.com/vapor/websocket-kit.git", from: "2.0.0"),
    ],
    targets: [
      .target(name: "Project", dependencies: ["WebSocket", ... ])
    ]
)

Use import WebSocketKit to access the APIs.

The rest of this guide will give you an overview of what is available in the WebSocket package. As always, feel free to visit the API docs for more in-depth information.