Skip to main content

Hello, World!

Enter your name in a form, send it to the server and get a greeting in return.

http://localhost:8080/hello-world

Go service

Service interface defintion

package helloworld

type Service interface {
Hello(name string) (greeting string)
}

Service implementation

package server

import "github.com/foomo/gotsrpc-playground/server/services/helloworld"

type helloWorldService struct {
}

func NewHelloWorld() helloworld.Service {
return &helloWorldService{}
}

func (s *helloWorldService) Hello(name string) (greeting string) {
return "Hello " + name
}

Service proxy instantiation

proxyHelloWorld := helloworld.NewDefaultServiceGoTSRPCProxy(server.NewHelloWorld())

Using the service proxy as a http handler

proxyHelloWorld.ServeHTTP(w, r)

Next.js TypeScript client

Using the client in a Next.js page

// construct client
const client = getClient(ServiceClient);

const HelloWorld = () => {
// state to hold greetings, that were returned from the server
const [greeting, setGreeting] = useState<string | null>(null);

// thi method uses the generated client to call the srver
const callHelloWorld = (name: string) => {
client
.hello(name)
.then((greeting) => setGreeting(greeting))
.catch((e) => console.error("an error has occurred", e));
};