Awesome Go

Pitaya

CategoryGame Development
SubcategoryGame Development
Stars2,853

Scalable game server framework with clustering support and client libraries for iOS, Android, Unity and others through the C SDK

About Pitaya

Pitaya

Build Status Coverage Status GoDoc Docs MIT licensed

Game server framework for building the real-time backend of multiplayer games, maintained by Wildlife Studios.

This branch is Pitaya v3 (module github.com/topfreegames/pitaya/v3), currently in beta. The previous major version lives on the v2 branch.

What does the module do?

Pitaya keeps a live connection open with every player, receives their requests, runs the game code that answers them, and pushes messages back to the player whenever something changes on the server.

A game built with Pitaya is usually split into several types of servers, for example one that players connect to and others that run rooms, matches or social features. Pitaya finds those servers, routes each request to the right one, and lets them call each other.

What problem is solved?

Every online game needs the same base before any game feature exists: accept connections, keep track of who is connected, route messages, talk between servers, and report metrics. Writing that base again for each game is slow and error-prone.

Pitaya gives teams that base as a library, so they write only the game's own logic and can grow from a single server to a cluster without rewriting it.

Recommended Integration Phase: Prototyping

Pitaya is the foundation the game's backend code is written on, so it should be adopted at the prototyping phase. A prototype can run as a single server with no external dependencies, and move to cluster mode later without changing its handlers.

Dependencies

In standalone mode Pitaya has no external dependencies. In cluster mode it needs:

DependencyUsed forRequired
etcdService discovery: servers register themselves and find each otherYes, in cluster mode
NATSDefault transport for server-to-server calls and pushesYes, unless gRPC is used
gRPCAlternative transport for server-to-server callsNo
Prometheus / StatsDMetrics reportingNo
OpenTelemetryDistributed tracing (e.g. Jaeger)No
RedisBackground workers with reliable RPCsNo

Players connect through the client SDKs:

  • libpitaya: C client library for iOS, Android, Unity and others.
  • libpitaya-cluster: lets servers written in C# and C++ join a Pitaya cluster.

How is the problem solved?

Every Pitaya server has a type (e.g. connector, room) and is either a frontend or a backend:

  • Frontend servers accept player connections over TCP or WebSocket and hold the player's session.
  • Backend servers are not reachable by players. They receive work from other servers through RPC.

Game code is organized in components. A component registered as a handler answers player requests; one registered as a remote answers calls from other servers. Requests use the route serverType.service.method: when a frontend receives a request for another server type, it forwards it to a server of that type picked through service discovery (randomly by default, or by a custom route function).

Backends can still act on a player: pushes, kicks and session changes travel back to the frontend that owns the connection. Messages are serialized as JSON or Protobuf, and custom serializers can be plugged in.

Most building blocks (service discovery, RPC transport, serializer, metrics reporters, groups, router, packet codec) are interfaces with default implementations, and can be replaced when the server is built.

Getting Started

Prerequisites

  • Go >= 1.25.4
  • Docker with Compose (optional, to run etcd and NATS locally)

Installing

Add Pitaya to your Go module:

go get github.com/topfreegames/pitaya/v3

To work on Pitaya itself, clone the repository and fetch its submodules:

git clone https://github.com/topfreegames/pitaya.git
cd pitaya
make setup

Running an example cluster

Start etcd:

cd examples/testing && docker compose up -d etcd && cd -

Run a frontend (connector) and a backend (room) server, each in its own terminal:

make run-cluster-grpc-example-connector
make run-cluster-grpc-example-room

Build pitaya-cli, the REPL client in this repository, and send a request:

make build
./build/pitaya-cli
Pitaya REPL Client
>>> connect localhost:3250
connected!
>>> request room.room.entry
>>> sv->{"code":0,"result":"ok"}

More examples live in examples/demo:

  • chat: chat room over WebSocket.
  • cluster: frontend and backend servers communicating over NATS.
  • cluster_grpc: the same cluster using gRPC.
  • worker: background jobs with reliable RPCs.
  • pipeline: handler hooks that run before and after requests.
  • rate_limiting: limiting requests per connection.
  • custom_metrics: reporting your own metrics.

Running the tests

make test          # unit tests
make e2e-test      # end-to-end tests over NATS and gRPC (needs Docker)

To load test a Pitaya server, see xk6-pitaya.

Additional information

Security

If you have found a security vulnerability, please email [email protected].

Acknowledgements

  • nano authors, for the framework Pitaya is based on.
  • pomelo authors, for the inspiration on the distributed design and protocol.

License

MIT License

Frequently Asked Questions

What is Pitaya?

Pitaya is a Game Development library for the Go programming language. Scalable game server framework with clustering support and client libraries for iOS, Android, Unity and others through the C SDK

How many GitHub stars does Pitaya have?

Pitaya has 2,853 GitHub stars in the directory's latest synchronization.

How do I install Pitaya?

Install Pitaya with the Go module system using `go get topfreegames/pitaya`. Check the repository for the current installation instructions.

What category does Pitaya belong to?

Pitaya is listed under Game Development, specifically Game Development.

← Back to Game Development