{"id":21946175,"url":"https://github.com/grdsdev/swift-websocket","last_synced_at":"2025-06-11T23:03:31.058Z","repository":{"id":264703794,"uuid":"891206603","full_name":"grdsdev/swift-websocket","owner":"grdsdev","description":"A WebSocket interface for Swift","archived":false,"fork":false,"pushed_at":"2025-03-17T13:24:56.000Z","size":66,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T14:27:26.381Z","etag":null,"topics":["swift","websocket","websocket-client"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grdsdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-19T23:13:33.000Z","updated_at":"2025-03-17T13:02:49.000Z","dependencies_parsed_at":"2024-11-25T21:37:14.896Z","dependency_job_id":"b7226789-c826-4cd0-b1ee-be9651842ad1","html_url":"https://github.com/grdsdev/swift-websocket","commit_stats":null,"previous_names":["grdsdev/swift-websocket"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/grdsdev/swift-websocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grdsdev%2Fswift-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grdsdev%2Fswift-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grdsdev%2Fswift-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grdsdev%2Fswift-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grdsdev","download_url":"https://codeload.github.com/grdsdev/swift-websocket/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grdsdev%2Fswift-websocket/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259360729,"owners_count":22845817,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["swift","websocket","websocket-client"],"created_at":"2024-11-29T04:29:12.299Z","updated_at":"2025-06-11T23:03:31.037Z","avatar_url":"https://github.com/grdsdev.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swift-websocket\n\nA lightweight, protocol-based WebSocket interface for Swift applications. This\nlibrary provides a clean abstraction for WebSocket connections with both real\nand mock implementations for testing.\n\n## Features\n\n- Protocol-based WebSocket interface\n- Foundation-based implementation using URLSession\n- Mock WebSocket implementation for testing\n- Support for text and binary messages\n- Async/await API support\n- Comprehensive error handling\n- Cross-platform support (iOS, macOS, tvOS, watchOS)\n\n## Installation\n\nAdd the following to your `Package.swift`:\n\n```swift\ndependencies: [\n  .package(url: \"https://github.com/grdsdev/swift-websocket.git\", from: \"0.0.1\")\n]\n```\n\nThen add the dependencies to your target:\n\n```swift\n.target(\n  name: \"YourTarget\",\n  dependencies: [\n    .product(name: \"WebSocket\", package: \"swift-websocket\"),\n    .product(name: \"WebSocketFoundation\", package: \"swift-websocket\")\n  ]\n)\n```\n\n## Usage\n\n### Basic Usage\n\n```swift\nimport WebSocket\nimport WebSocketFoundation\n\n// Connect to a WebSocket server\nlet ws: any WebSocket = try await URLSessionWebSocket.connect(\n  to: URL(string: \"wss://echo.websocket.org/.ws\")!\n)\n\n// Set up event handler\nws.onEvent = { event in\n  switch event {\n  case .text(let text):\n    print(\"Received text: \\(text)\")\n  case .binary(let data):\n    print(\"Received binary data: \\(data)\")\n  case .close(let code, let reason):\n    print(\"Connection closed: \\(code ?? 0) - \\(reason)\")\n  }\n}\n\n// Send messages\nws.send(\"Hello, WebSocket!\")\n\n// Send binary data\nlet data = \"Hello\".data(using: .utf8)!\nws.send(data)\n\n// Close the connection\nws.close(code: 1000, reason: \"Normal closure\")\n```\n\n### Using AsyncStream\n\nThe library provides an `AsyncStream` interface for handling WebSocket events:\n\n```swift\n// Using async/await with events stream\nfor await event in ws.events {\n  switch event {\n  case .text(let text):\n    print(\"Received: \\(text)\")\n  case .binary(let data):\n    print(\"Received binary: \\(data)\")\n  case .close(let code, let reason):\n    print(\"Connection closed: \\(code ?? 0) - \\(reason)\")\n  }\n}\n```\n\n### Testing with `FakeWebSocket`\n\nThe library includes a `FakeWebSocket` implementation for testing purposes. It\nallows you to create a pair of connected WebSockets for testing your\nWebSocket-based features:\n\n```swift\nimport XCTest\nimport WebSocket\n\nclass YourTests: XCTestCase {\n  func testWebSocketCommunication() {\n    // Create a pair of connected fake WebSockets\n    let (client, server) = FakeWebSocket.fakes()\n    \n    // Set up expectations\n    let expectation = XCTestExpectation(description: \"Message received\")\n    \n    // Handle server-side events\n    server.onEvent = { event in\n      if case .text(let text) = event {\n        XCTAssertEqual(text, \"Hello\")\n        expectation.fulfill()\n      }\n    }\n    \n    // Send message from client\n    client.send(\"Hello\")\n    \n    // Verify message was received\n    wait(for: [expectation], timeout: 1.0)\n    \n    // Verify sent/received events\n    XCTAssertEqual(client.sentEvents.count, 1)\n    XCTAssertEqual(server.receivedEvents.count, 1)\n  }\n}\n```\n\n## Error Handling\n\nThe library provides a `WebSocketError` enum for handling connection-related\nerrors:\n\n```swift\ndo {\n  let ws = try await URLSessionWebSocket.connect(to: url)\n  // Use websocket\n} catch let error as WebSocketError {\n  switch error {\n  case .connection(let message, let underlyingError):\n    print(\"Connection failed: \\(message) - \\(underlyingError)\")\n  }\n}\n```\n\n## Platform Support\n\n- iOS 13.0+\n- macOS 10.15+\n- tvOS 13.0+\n- watchOS 6.0+\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrdsdev%2Fswift-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrdsdev%2Fswift-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrdsdev%2Fswift-websocket/lists"}