# Can I get some help to implement a Go Realtime Client API

**URL:** <https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473>\
**Category:** Client Libraries\
**Tags:** golang\
**Created:** [January 18, 2020, 10:29am UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473 "2020-01-18T10:29:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcusscorpius](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marcusscorpius](https://forum.heroiclabs.com/u/marcusscorpius)\
**Post date:** [January 18, 2020, 10:29am UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/1 "2020-01-18T10:29:30Z")

</div>

I’m developing a cli in Go for interacting with a nakama game.

I have the non-realtime component working using `apigrpc.NakamaClient`. The game also uses streams, and it’s not clear how to implement these client-side in Go.

I’d like to implement the Socket functionality found in the javascript client [socket.ts](https://github.com/heroiclabs/nakama-js/blob/master/src/socket.ts) in Go.

In particular, I need to implement `MatchData` and `StreamData` handlers (and possibly others later). I would also prefer using **gRPC** as the network transport, but websockets are fine if gRPC is not possible.

This issue [golang client availability #346](https://github.com/heroiclabs/nakama/issues/346) alludes to using `github.com/nakama-common/rtapi` for real-time functionality, but I can’t find any examples of how to actually use it.

Can I get some help implementing an simple client that can _send_/_receive_ `MatchData` and `StreamData`? Thanks.

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [January 19, 2020, 9:57pm UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/2 "2020-01-19T21:57:47Z")

</div>

Hi @marcusscorpius welcome 👋

> In particular, I need to implement `MatchData` and `StreamData` handlers (and possibly others later). I would also prefer using **gRPC** as the network transport, but websockets are fine if gRPC is not possible.

It’s not possible to use the gRPC protocol with the realtime socket features in Nakama because gRPC is not fast enough (at the network level) to implement multiplayer or our streams gameplay APIs. Instead we designed our socket layer to support different network transports.

> Can I get some help implementing an simple client that can _send_ / _receive_`MatchData` and `StreamData` ? Thanks.

Sure. We don’t have any documentation on how to implement the socket portion of the Nakama API (except for the [client sdks](https://github.com/heroiclabs?utf8=%E2%9C%93&q=client&type=&language=) which are all open-source) but I can guide you through how it works:

- The `/ws?token="session token"` endpoint in the server expects a WebSocket protocol upgrade message which will convert the HTTP request into an open socket connection.
- The socket expects every message to have an envelope which is [this](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L37) protobuf message.
- The socket can accept messages in binary or text mode but you cannot switch between one and the other.
- The envelope message contains the marker for the content of the inner message to be consumed. This allows chat, multiplayer, status events, and other realtime messages to be multiplexed over a single socket.
- For the realtime multiplayer messages you need to look at: [Match](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L256), [MatchCreate](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L272), [MatchData](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L275), [MatchDataSend](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L289), [MatchJoin](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L303), [MatchLeave](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L315), [MatchPresenceEvent](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L321), [MatchmakerAdd](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L331), [MatchmakerMatched](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L345), [MatchmakerRemove](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L371), and [MatchmakerTicket](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L377).
- For stream messages you need to look at: [StreamData](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L441), and [StreamPresenceEvent](https://github.com/heroiclabs/nakama-common/blob/master/rtapi/realtime.proto#L453).

Hope this helps. We’d like to have an official Go client implementation maintained in the source tree for the codebase at some point. All contributions are welcome.

---

<div class="post-metadata">

**Author:** ![matthiasbruns](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/matthiasbruns/32/385_2.png) [@matthiasbruns](https://forum.heroiclabs.com/u/matthiasbruns)\
**Post date:** [June 11, 2021, 8:36am UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/3 "2021-06-11T08:36:38Z")

</div>

Hi @novabyte , I hope you will reply to an old topic, but I didn’t want to create a new one.  
I am pretty lost when it’s about realtime and go. So did I understand you correctly that we need to connect to a http websocket from a microservice to enable realtime features through that microservice?

Is there a way to generate those calls based on the realtime.proto? Or is this manual work? 😃

I am trying to create a match through our golang microservice. The problem is, that it’s not covered in the NakamaClient interface in apigrpc. There is a proto definition for the realtime api in nakama commons but I cannot find any service or rcp endpoints for it

> <https://github.com/heroiclabs/nakama-common/blob/6fdd0c82aa7464459aba6776de092933144774a0/rtapi/realtime.pb.go>

How would I create a match from a golang microservice then?

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [June 13, 2021, 10:14pm UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/4 "2021-06-13T22:14:11Z")

</div>

@matthiasbruns No worries. The thread bump is a good reminder that we’ve not provided the Go client SDK which I think will be of great value at this point to game teams that want to communicate with the Nakama APIs server to server.

> Is there a way to generate those calls based on the realtime.proto? Or is this manual work?

There’s no way to code generate the socket object/type with its signatures and inputs/outputs in the same way as we’ve done with our GRPC API. It has to be hand written unfortunately. There’s an open issue to create a formal SDK for Go developers which you could bump the priority on.

> How would I create a match from a golang microservice then?

You could create the match via a socket (or non-socket) RPC call but I’m curious at your use case. You want the match to be stared via another Go service but would game clients still connect direct to the Nakama servers to join and participate in the match?

---

<div class="post-metadata">

**Author:** ![matthiasbruns](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/matthiasbruns/32/385_2.png) [@matthiasbruns](https://forum.heroiclabs.com/u/matthiasbruns)\
**Post date:** [June 14, 2021, 7:03am UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/5 "2021-06-14T07:03:33Z")

</div>

@novabyte The use-case is a bit tricky. We would like to build something like skillz with Nakama as our backend but additional logic in microservices. For example quota for non-released games. We also want to have a central account system and use nakama custom auth to create player accounts per game (something like steam, where you also have one central account). This also includes virtual goods and real money. So we need some kind of middleware to handle the communication from the clients to a specific nakama instance based on the game they play. This is all in evauluation and maybe we will ditch the realtime proxy services completely, but I need to check first, if it is doable. As afar as I can see, Nakama uses the client ip for realtime communication, which would not work in our case then.

---

<div class="post-metadata">

**Author:** ![matthiasbruns](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/matthiasbruns/32/385_2.png) [@matthiasbruns](https://forum.heroiclabs.com/u/matthiasbruns)\
**Post date:** [June 14, 2021, 7:05am UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/6 "2021-06-14T07:05:08Z")

</div>

Maybe the better way is to customize/extend Nakama directly with the features we need. Still all evaluation 🙂

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [June 14, 2021, 1:37pm UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/7 "2021-06-14T13:37:23Z")

</div>

@matthiasbruns I think a customization or fork of Nakama would be a mistake because you’d have to maintain synchronization with the mainline codebase that we maintain. All that’s missed right now is a socket implementation as part of an official Go SDK. You could always sponsor or contribute the work to the codebase. 😃

---

<div class="post-metadata">

**Author:** ![matthiasbruns](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/matthiasbruns/32/385_2.png) [@matthiasbruns](https://forum.heroiclabs.com/u/matthiasbruns)\
**Post date:** [June 21, 2021, 1:54pm UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/8 "2021-06-21T13:54:27Z")

</div>

Ah no I meant injecting Nakama scripts 😃  
But yes, the go client is currently WIP on our side.

---

<div class="post-metadata">

**Author:** ![tomasz](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@tomasz](https://forum.heroiclabs.com/u/tomasz)\
**Post date:** [January 11, 2023, 9:22pm UTC](https://forum.heroiclabs.com/t/can-i-get-some-help-to-implement-a-go-realtime-client-api/473/9 "2023-01-11T21:22:43Z")

</div>

We made a Go client with realtime/websocket functionality available to the public. See the post here: [Go Client Library/Package](https://forum.heroiclabs.com/t/go-client-library-package/3409)
