Skip to content

Cross-platform Json RPC library for Swift with WebSocket support

License

Notifications You must be signed in to change notification settings

tesseract-one/JsonRPC.swift

Repository files navigation

JsonRPC.swift

🐧 linux: ready GitHub license Build Status GitHub release SPM compatible CocoaPods version Platform OS X | iOS | tvOS | watchOS | Linux

Cross-platform JsonRPC client implementation with HTTP and WebSocket support

Linux WebSocket support

WebSocket should work on Linux when this bug will be fixed: Issue #4730 You can use 0.1.1 version for now, which uses SwiftNIO for WebSocket.

Getting started

Installation

Add the following dependency to your Package.swift:

.package(url: "https://github.com/tesseract-one/JsonRPC.swift.git", from: "0.2.0")

Run swift build and build your app.

Add the following to your Podfile:

pod 'JsonRPC.swift', '~> 0.2.0'

Then run pod install

Examples

HTTP connection

import Foundation
import JsonRPC

let rpc = JsonRpc(.http(url: URL(string: "https://api.avax-test.network/ext/bc/C/rpc")!), queue: .main)

rpc.call(method: "web3_clientVersion", params: Params(), String.self, String.self) { res in
  print(try! res.get())
}

// Or with async/await (Swift 5.5+)
let res = await rpc.call(method: "web3_clientVersion", params: Params(), String.self, String.self)
print(res)

WebSocket connection

import Foundation
import JsonRPC

let rpc = JsonRpc(.ws(url: URL(string: "wss://api.avax-test.network/ext/bc/C/ws")!), queue: .main)

rpc.call(method: "web3_clientVersion", params: Params(), String.self, String.self) { res in
  print(try! res.get())
}

// Or with async/await (Swift 5.5+)
let res = await rpc.call(method: "web3_clientVersion", params: Params(), String.self, String.self)
print(res)

Notifications

import Foundation
import JsonRPC
// This will allow dynamic JSON parsing 
// https://github.com/tesseract-one/Serializable.swift
import Serializable

// Notification body structure
struct NewHeadsNotification: Decodable {
    let subscription: String
    let result: SerializableValue
}

class Delegate: ConnectableDelegate, NotificationDelegate, ErrorDelegate {
  // Connectable Delegate. Will send connection updates
  public func state(_ state: ConnectableState) {
    print("Connection state: \(state)")
  }

  // Error delegate. Will send global errors (uknown response id, etc.)
  public func error(_ error: ServiceError) {
    print("Error: \(error)")
  }

  public func notification(method: String, params: Parsable) {
    let notification = try! params.parse(to: NewHeadsNotification.self).get()!
    print("\(method): \(notification)")
  }
}

// Create RPC
let rpc = JsonRpc(.ws(url: URL(string: "wss://main-rpc.linkpool.io/ws")!, autoconnect: false), queue: .main)

// Set delegate. Notification and statuses will be forwarded to it
rpc.delegate = Delegate()

// Connect to the server
rpc.connect()

// Call subsribe method.
// You can use Params() for array of Encodable parameters or provide own custom Encodable value.
rpc.call(method: "eth_subscribe", params: Params("newHeads"), String.self, SerializableValue.self) { res in
    print(try! res.get())
}

// Or with async/await (Swift 5.5+)
let res = await rpc.call(method: "eth_subscribe", params: Params("newHeads"), String.self, SerializableValue.self)
// or let res: String = await rpc.call(method: "eth_subscribe", params: Params("newHeads"), SerializableValue.self)
print(res)

Author

License

JsonRPC.swift is available under the Apache 2.0 license. See the LICENSE file for more information.