rsocket / rsocket-go

rsocket-go implementation
Apache License 2.0
508 stars 46 forks source link
go golang reactive rsocket

rsocket-go

logo

GitHub Workflow Status codecov Go Report Card GoDoc License GitHub Release

rsocket-go is an implementation of the RSocket protocol in Go.

Features

Install

Minimal go version is 1.11.

$ go install github.com/rsocket/rsocket-go/cmd/rsocket-cli@latest

Quick Start

Start an echo server

package main

import (
    "context"
    "log"

    "github.com/rsocket/rsocket-go"
    "github.com/rsocket/rsocket-go/payload"
    "github.com/rsocket/rsocket-go/rx/mono"
)

func main() {
    err := rsocket.Receive().
        Acceptor(func(ctx context.Context, setup payload.SetupPayload, sendingSocket rsocket.CloseableRSocket) (rsocket.RSocket, error) {
            // bind responder
            return rsocket.NewAbstractSocket(
                rsocket.RequestResponse(func(msg payload.Payload) mono.Mono {
                    return mono.Just(msg)
                }),
            ), nil
        }).
        Transport(rsocket.TCPServer().SetAddr(":7878").Build()).
        Serve(context.Background())
    log.Fatalln(err)
}

Connect to echo server

package main

import (
    "context"
    "log"

    "github.com/rsocket/rsocket-go"
    "github.com/rsocket/rsocket-go/payload"
)

func main() {
    // Connect to server
    cli, err := rsocket.Connect().
        SetupPayload(payload.NewString("Hello", "World")).
        Transport(rsocket.TCPClient().SetHostAndPort("127.0.0.1", 7878).Build()).
        Start(context.Background())
    if err != nil {
        panic(err)
    }
    defer cli.Close()
    // Send request
    result, err := cli.RequestResponse(payload.NewString("你好", "世界")).Block(context.Background())
    if err != nil {
        panic(err)
    }
    log.Println("response:", result)
}

NOTICE: more server examples are Here

Advanced

rsocket-go provides TCP/Websocket transport implementations by default. Since v0.6.0, you can use core package to implement your own RSocket transport. I created an example project which show how to implement an unofficial QUIC transport. You can see rsocket-transport-quic if you are interested.

TODO