|
7 | 7 |
|
8 | 8 | # Observable WebSocket Client ⚡ |
9 | 9 | A Swift package that establishes [WebSocket connections](https://en.wikipedia.org/wiki/WebSocket) and publishes received messages and errors from an [Observable Object](https://developer.apple.com/documentation/combine/observableobject). |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Initialization |
| 14 | + |
| 15 | +```swift |
| 16 | +let websocketURL = URL(string: "wss://websocket-endpoint.com")! |
| 17 | +let wsClient = ObservableWebSocketClient(websocketURL: websocketURL) |
| 18 | + |
| 19 | +/* |
| 20 | + A `URLSessionWebSocketTask` is created and resumed just after the |
| 21 | + client initialization. Nothing else is required at this point. |
| 22 | + */ |
| 23 | +``` |
| 24 | + |
| 25 | +### Observation |
| 26 | + |
| 27 | +#### Connection |
| 28 | + |
| 29 | +```swift |
| 30 | +wsClient |
| 31 | + .$isConnected |
| 32 | + .sink { isConnected in |
| 33 | + print("isConnected: \(isConnected)") |
| 34 | + } |
| 35 | + .store(in: &cancellables) |
| 36 | +``` |
| 37 | + |
| 38 | +#### Messages |
| 39 | + |
| 40 | +```swift |
| 41 | +wsClient |
| 42 | + .$codableMessage |
| 43 | + .compactMap{ $0 } |
| 44 | + .sink { codableMessage in |
| 45 | + print("Message: \(codableMessage.messageAsString())") |
| 46 | + // codableMessage.message returns the original `URLSessionWebSocketTask.Message` |
| 47 | + } |
| 48 | + .store(in: &cancellables) |
| 49 | +``` |
| 50 | + |
| 51 | +#### Errors |
| 52 | + |
| 53 | +```swift |
| 54 | +wsClient |
| 55 | + .$codableError |
| 56 | + .compactMap{ $0 } |
| 57 | + .sink { codableError in |
| 58 | + print("Error: \(codableError.description)") |
| 59 | + } |
| 60 | + .store(in: &cancellables) |
| 61 | +``` |
| 62 | + |
| 63 | +### Ping/Pong 🏓 |
| 64 | + |
| 65 | +#### Ping Message |
| 66 | +Passing in a `pingTimerInterval` will cause a timer to continuously send the given `pingMessage` to the WS server, keeping the connection alive: |
| 67 | + |
| 68 | +```swift |
| 69 | +let websocketURL = URL(string: "wss://endpoint.com")! |
| 70 | +let wsClient = ObservableWebSocketClient( |
| 71 | + websocketURL: websocketURL, |
| 72 | + pingTimerInterval: 18, // Every 18 seconds |
| 73 | + pingMessage: "{\"type\": \"ping\"}" // The format is defined by the WS server |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +#### Ping Message with Generated ID |
| 78 | +To generate a unique ID for the ping-type message, use the closure in `pingMessageWithGenerateId`. The closure takes a `String` (the generated unique ID), returns its modified version incorporating that ID, and sends the message to the WS server. As in the above, these steps are repeated continuously, generating unique IDs each time to keep the connection alive: |
| 79 | + |
| 80 | +```swift |
| 81 | +let websocketURL = URL(string: "wss://endpoint.com")! |
| 82 | +let wsClient = ObservableWebSocketClient( |
| 83 | + websocketURL: websocketURL, |
| 84 | + pingTimerInterval: 18, // Every 18 seconds |
| 85 | + pingMessageWithGeneratedId: { generatedId in |
| 86 | + "{\"id\": \"\(generatedId)\", \"type\": \"ping\"}" // The format is defined by the WS server |
| 87 | + } |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +## Integration |
| 92 | +### Xcode |
| 93 | +Use Xcode's [built-in support for SPM](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app). |
| 94 | + |
| 95 | +*or...* |
| 96 | + |
| 97 | +### Package.swift |
| 98 | +In your `Package.swift`, add `ObservableWebSocketClient` as a dependency: |
| 99 | + |
| 100 | +```swift |
| 101 | +dependencies: [ |
| 102 | + .package( |
| 103 | + url: "https://github.com/backslash-f/observable-websocket-client", |
| 104 | + from: "1.0.0" |
| 105 | + ) |
| 106 | +] |
| 107 | +``` |
| 108 | + |
| 109 | +Associate the dependency with your target: |
| 110 | + |
| 111 | +```swift |
| 112 | +targets: [ |
| 113 | + .target( |
| 114 | + name: "YourAppName", |
| 115 | + dependencies: [ |
| 116 | + .product( |
| 117 | + name: "ObservableWebSocketClient", |
| 118 | + package: "observable-websocket-client" |
| 119 | + ) |
| 120 | + ] |
| 121 | + ) |
| 122 | +] |
| 123 | +``` |
| 124 | + |
| 125 | +Run: `swift build` |
| 126 | + |
0 commit comments