https://github.com/tsukinoko-kun/gopherpc
GopheRPC is a RPC library that lets you call Go server functions from your JavaScript (browser) clients
https://github.com/tsukinoko-kun/gopherpc
golang javascript rpc rpc-framework rpc-library
Last synced: about 2 months ago
JSON representation
GopheRPC is a RPC library that lets you call Go server functions from your JavaScript (browser) clients
- Host: GitHub
- URL: https://github.com/tsukinoko-kun/gopherpc
- Owner: tsukinoko-kun
- License: other
- Created: 2024-11-17T00:14:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-04T22:28:03.000Z (about 1 year ago)
- Last Synced: 2025-10-10T16:44:37.601Z (9 months ago)
- Topics: golang, javascript, rpc, rpc-framework, rpc-library
- Language: Go
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GopheRPC
GopheRPC is a RPC library that lets you call Go server functions from your JavaScript (browser) clients without any additional dependencies or code generation.
Server functions can be added on runtime (even after the client has connected to the server) and can return any value or error that is JSON serializable.
Return values on client side are wrapped in a `Promise`.

```go
package main
import (
"context"
"net/http"
"github.com/tsukinoko-kun/gopherpc/v2"
)
func main() {
gopherpc.Register("foo", func(ctx context.Context, args []any) (any, error) {
return "bar", nil
})
// Use HandleFunc, Handle or Get depending on the mux you are using.
// Tested with net/http and chi router.
gopherpc.HandleFunc(http.DefaultServeMux)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`
GopherPC
` +
gopherpc.ImportJs() + // script tag that imports the gopherpc.js runtime
`Call foo
`))
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
```
## Args
Use `gopherpc.Unmarshal` to parse your `[]any` args into a struct.
The order of the args is the same as the order of the fields in the struct.
You can override this by using the `index` tag.
```go
type Args struct {
Name string
Email string
Loaded bool `index:"-"`
Phone string `index:"2"`
}
```
```go
gopherpc.Register("foo", func(ctx context.Context, args []any) (any, error) {
var a Args
if err := gopherpc.Unmarshal(args, &a); err != nil {
return nil, err
}
return a.Name + " " + a.Email + " " + a.Phone, nil
})
```