JSONRPC

0.9.0

Swift library for JSON-RPC
ChimeHQ/JSONRPC

What's New

v0.9.0

2023-10-20T11:17:21Z

What's Changed

  • Feature/stdio pipe datachannel by @koliyo in #9
  • Actor DataChannel by @koliyo in #10
  • Unified event sequence

New Contributors

  • @koliyo made their first contribution in #9

Full Changelog: 0.8.1...0.9.0

Build Status License Platforms Documentation

JSONRPC

A simple Swift library for JSON-RPC

Features:

  • type-safety
  • flexible data transport support
  • concurrency support

Integration

dependencies: [
    .package(url: "https://github.com/ChimeHQ/JSONRPC", from: "0.9.0")
]

Usage

The core type you'll use is JSONRPCSession. It requires you set up a DataChannel object that handles reading and writing raw data.

let channel = DataChannel(...)
let session = JSONRPCSession(channel: channel)

let params = "hello" // any Encodable
let response: Decodable = try await session.sendRequest(params, method: "my_method")

Task {
    for await (request, handler, data) in session.requestSequence {
        // inspect request, possibly re-decode with more specific type,
        // and reply using the handler
    }
}

Task {
    for await (notification, data) in session.notificationSequence {
        // inspect notification
    }
}

DataChannel

The closures on the DataChannel allow different transport mechanisms to be used. The JSONRPC package provides a few basic variants:

  • Predefined messages channel
    • A channel that delivers a static set of messages
    • Usage: let channel = await DataChannel.predefinedMessagesChannel(with: messages)
  • Stdio channel
    • Using stdout + stdin as message transport.
    • Note: When using this transport, make sure no non-protocol messages are sent to stdout, eg using print
    • Usage: let channel = DataChannel.stdioPipe()
  • Actor channel
    • Using swift actors to pass messages.
    • Can eg be useful for testing, where both client and server are implemented in swift and running in the same process.
    • Usage: let (clientChannel, serverChannel) = DataChannel.withDataActor()

Contributing and Collaboration

I'd love to hear from you! Get in touch via an issue or pull request.

I prefer collaboration, and would love to find ways to work together if you have a similar project.

I prefer indentation with tabs for improved accessibility. But, I'd rather you use the system you want and make a PR than hesitate because of whitespace.

By participating in this project you agree to abide by the Contributor Code of Conduct.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Dec 21 2024 08:08:15 GMT-1000 (Hawaii-Aleutian Standard Time)