Cross-platform JsonRPC client implementation with HTTP and WebSocket support

Overview

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

Getting started

Installation

Package Manager

Add the following dependency to your Package.swift:

.package(url: "https://github.com/tesseract-one/JsonRPC.swift.git", from: "0.1.0")
// you can enable Serializable support for dynamic error parsing (add dependency to your target too)
// .package(url: "https://github.com/tesseract-one/Serializable.swift.git", from: "0.2.0")

Run swift build and build your app.

CocoaPods

Add the following to your Podfile:

pod 'JsonRPC.swift', '~> 0.1.0'
# you can enable Serializable support for dynamic error parsing
# pod 'JsonRPC.swift/Serializable', '~> 0.1.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())
}

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())
}

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, ServerDelegate, 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.
// You can omit last parameter if you have Serializable dependency. This will set error data to SerializableValue type.
rpc.call(method: "eth_subscribe", params: Params("newHeads"), String.self) { res in
    print(try! res.get())
}

Author

License

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

You might also like...
OBSwiftSocket is a Swift library to be used for communication with OBS Studio via obs-websocket (v5).

OBSwiftSocket OBSwiftSocket is a Swift library to be used for communication with OBS Studio via obs-websocket (v5). obs-websocket v5 specification: ht

A barebones Swift HTTP client with automatic JSON response parsing.

HTTP Client A barebones Swift HTTP client with automatic JSON response parsing. Installation Add HTTP Client as a dependency through Xcode or directly

 Postie - The Next-Level HTTP API Client using Combine
Postie - The Next-Level HTTP API Client using Combine

Postie - The Next-Level HTTP API Client using Combine Postie is a pure Swift library for building URLRequests using property wrappers.

📡 RealHTTP is a lightweight yet powerful client-side HTTP library.
📡 RealHTTP is a lightweight yet powerful client-side HTTP library.

RealHTTP RealHTTP is a lightweight yet powerful client-side HTTP library. Our goal is make an easy to use and effortless http client for Swift. Featur

The HTTP library used by the Spotify iOS client
The HTTP library used by the Spotify iOS client

Authentication and back-off logic is a pain, let's do it once and forget about it! This is a library that allows you to centralise this logic and forg

HTTPClient - HTTP Client With Swift

HTTPClient Ex. Search Repository import HTTPClient let url: URL = .init(string:

Impervious is a privacy and security-focused browser with native DANE support and a decentralized p2p light client.

Impervious iOS The first browser with support for native DNS-Based Authentication of Named Entities (DANE) with true downgrade protection, and the fir

Beacon is a privacy and security-focused browser with native DANE support and a decentralized p2p light client.

Beacon iOS The first browser with support for native DNS-Based Authentication of Named Entities (DANE) with true downgrade protection, and the first b

Twitter-Client - A twitter client that allow users to view tweets on their iphone

Project 3 - Twitter Client Name of your app is a basic twitter app to read your

Comments
  • params is array  [Encodable] how to call

    params is array [Encodable] how to call

           let   blockHash:String = "xx";
           let txGlobalIndex:Int = 1;
           let txGlobalIndex:Int = 1;
           let params = [blockHash, txGlobalIndex, eventIndex] 
    
            client.call(
                    method: "chain.get_transaction_proof",
                    params: params,
                    TransactionProof.self,
                    Array<SerializableValue>.self
            )
    

    expect params

    ["0x37e8dd4f432a1c3a7b6dfcaa90ebf2aafa0287678ffe4b8ad2373c5b48ffb20c", 231188, 1]

    opened by uvd 2
Releases(0.1.1)
Owner
Tesseract
connecting dApps and Wallets
Tesseract
A slim implementation of a websocket server using Swift and Vapor 4.0.

Swift Websocket Server Example using Vapor 4.0 This project includes a minimum working example for a websocket server written in Swift. To interact wi

Adrian Hupka 5 Sep 22, 2022
Conforming WebSocket (RFC 6455) client library for iOS and Mac OSX

SwiftWebSocket Conforming WebSocket (RFC 6455) client library for iOS and Mac OS

null 0 Dec 24, 2021
A conforming Objective-C WebSocket client library.

SocketRocket A conforming WebSocket (RFC 6455) client library for iOS, macOS and tvOS. Test results for SocketRocket here. You can compare to what mod

Facebook Incubator 9.4k Dec 27, 2022
Http - Demo for Http Layer

http Example To run the example project, clone the repo, and run pod install fro

null 0 Jan 24, 2022
iONess is HTTP Request Helper for iOS platform used by HCI iOS App

iONess iONess (iOS Network Session) is HTTP Request Helper for the iOS platform used by Home Credit Indonesia iOS App. It uses Ergo as a concurrent he

OSFE Homecredit Indonesia 1 Mar 28, 2022
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support

Networking was born out of the necessity of having a simple networking library that doesn't have crazy programming abstractions or uses the latest rea

Nes 1.3k Dec 17, 2022
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Always Right Institute 116 Aug 6, 2022
WebSocket(RFC-6455) library written using Swift

DNWebSocket Object-Oriented, Swift-style WebSocket Library (RFC 6455) for Swift-compatible Platforms. Tests Installation Requirements Usage Tests Conf

Gleb Radchenko 36 Jan 29, 2022
Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM

AQI Simple iOS app in Swift to show AQI for some cities using websocket using Combine + MVVM This app follows MVVM This app uses combine framework The

Amey Vikkram Tiwari 2 Nov 6, 2022
Enable WebSocket in OPC DA/AE Server with JSON return, first time ever

WebSocket4OPC Enable WebSocket in OPC DA/AE Server with JSON return, first time ever DCOM was developed more than 2 decades ago, wich was the pillar o

null 13 Dec 14, 2022