A Pusher Channels protocol client for Unity, in plain C#, for
Hibira and other Pusher-compatible servers. It runs in
the Editor, on desktop, Android and iOS, and in WebGL builds, where it uses
the browser's WebSocket through a small .jslib.
- Public, private and presence channels
- Client events (
client-*) between players, with the sender'suser_idon presence channels - Presence members, joins and leaves
- Reconnects with backoff and subscribes again; answers and sends pings
- Events on Unity's main thread, so you can touch GameObjects in handlers
HibiraRoom: game rooms with seats, a host, ready flags, settings, kick, start, and chat with typing indicators. It speaks the same events as @hibira-io/lobby and the Godot addon, so Unity, Godot and browser players can share a room.HibiraQueue: a matchmaking queue that matches players by rating and criteria, gives up after a timeout, and moves the match into a new room. Unity and@hibira-io/lobby0.2 players can wait in the same queue.- No dependencies on other packages
Requires Unity 2021.3 or later.
In Unity, open Window → Package Manager, click +, choose Add package from git URL…, and enter:
https://github.com/hibira-io/unity.git
To pin a version, add the tag: https://github.com/hibira-io/unity.git#v0.2.0.
Or add it to Packages/manifest.json yourself:
{
"dependencies": {
"io.hibira.realtime": "https://github.com/hibira-io/unity.git#v0.2.0"
}
}The package includes a Lobby sample (Package Manager → Hibira Realtime → Samples → Import).
using Hibira;
using UnityEngine;
public class Scores : MonoBehaviour
{
HibiraClient hibira;
void Start()
{
hibira = new HibiraClient("your-app-key"); // from the Hibira portal; never the secret
hibira.Connected += socketId => Debug.Log($"connected {socketId}");
hibira.Connect();
var scores = hibira.Subscribe("scores");
scores.Bind("updated", e => Debug.Log(e.DataJson));
}
void OnDestroy() => hibira.Dispose();
}The default host is ws.hibira.io on port 443 with TLS. For another
Pusher-compatible server, pass HibiraOptions with Host, Port and
UseTls.
You can also add the Hibira Connection component to a GameObject, fill in
the app key in the Inspector, and use GetComponent<HibiraConnection>().Client
from your scripts.
Event data arrives decoded: e.Data is a Dictionary<string, object>,
List<object>, long, double, string, bool or null, and e["field"]
reads a field. To read it into your own class, use JsonUtility through
DataAs<T>():
[System.Serializable]
public class Score { public string player; public int points; }
scores.Bind("updated", e =>
{
var score = e.DataAs<Score>();
Debug.Log($"{score.player}: {score.points}");
});Your backend signs these subscriptions with the app secret, exactly as for pusher-js, so any backend that already works with pusher-js or Laravel Echo works here. Point the client at it:
hibira = new HibiraClient(new HibiraOptions
{
AppKey = "your-app-key",
AuthEndpoint = "https://your-game.example/hibira/auth",
AuthHeaders = { ["Authorization"] = $"Bearer {sessionToken}" },
AuthParams = { ["player"] = playerId }, // extra form fields, if your endpoint wants them
});The endpoint gets a form POST with socket_id and channel_name and returns
{"auth": "...", "channel_data": "..."}. To sign some other way, set
Authorizer:
options.Authorizer = async (socketId, channelName) =>
{
var res = await myApi.SignChannel(socketId, channelName);
return new HibiraAuth(res.auth, res.channel_data); // or null to refuse
};Never ship the app secret in a game build: anyone can read it out of the build.
var party = hibira.Subscribe("presence-party-7");
party.Subscribed += _ => Debug.Log($"members: {party.Members.Count}");
party.MemberAdded += (id, info) => Debug.Log($"{id} joined");
party.MemberRemoved += (id, info) => Debug.Log($"{id} left");
party.SubscriptionError += (status, message) => Debug.Log($"refused: {status} {message}");
// Client events go to the other subscribers, not back to you. Turn on client
// events for the app in the Hibira portal (App → Settings).
party.Trigger("client-emote", new Dictionary<string, object> { ["emote"] = "wave" });
party.Bind("client-emote", e => ShowEmote(e.UserId, (string)e["emote"]));Trigger takes dictionaries, lists, numbers and strings, or any
[Serializable] class (written with JsonUtility).
var room = HibiraRoom.Join(hibira, "presence-room-42", new HibiraRoomOptions { Capacity = 4 });
room.Joined += (players, host) => Render(players);
room.MembersChanged += (players, joined, left) => Render(players);
room.HostChanged += (host, previous) => Debug.Log($"{host.Name} is the host now");
room.SettingsChanged += (settings, by) => ShowSettings(settings);
room.MessageReceived += m => AddChatLine(m.FromName, m.Text);
room.TypingChanged += ids => ShowTyping(ids);
room.Started += (data, by) => StartGame(data);
room.Full += capacity => Debug.Log("This room is full");
room.Kicked += (by, reason) => LeaveScreen();
room.SetReady(true);
room.Send("gl hf");
chatInput.onValueChanged.AddListener(_ => room.SendTyping());
if (room.IsHost)
{
room.SetSettings(new Dictionary<string, object> { ["map"] = "desert", ["rounds"] = 3 });
room.Start(new Dictionary<string, object> { ["seed"] = 1234 }, requireAllReady: true);
}
room.Players; // seat order: Id, Name, Info, Ready, Host, Me
room.Leave();Seats and the host come from the joined_at your auth endpoint signs into
user_info; authorizeRoom() in @hibira-io/lobby/server does that for Node,
and the lobby-js README shows
Laravel. The rules (who may send what, what happens when the host leaves, the
capacity race) are the same as in
@hibira-io/lobby. Game events
of your own go on the same channel: room.Channel.Trigger("client-move", …).
Instead of a room browser, players can wait in a queue. When enough players fit together, they all leave the queue and land in a new room.
var queue = HibiraQueue.Join(hibira, "presence-queue-duo", new HibiraQueueOptions
{
Size = 2, // players per match
RatingRange = 100, // rating difference allowed at first...
WidenBy = 50, // ...plus 50 for every 5 s of waiting
WidenEvery = 5,
MaxRange = 400,
Timeout = 60, // seconds before giving up
});
queue.Updated += (position, count) => ShowWaiting(position, count);
queue.Matched += match =>
{
// match.Room is a HibiraRoom on presence-match-<random id>, already joining
match.Room.Joined += (players, host) => GoToRoomScreen(match.Room);
};
queue.TimedOut += waited => ShowNoMatch();
cancelButton.onClick.AddListener(queue.Leave);There is no matchmaking server. The queue is a presence channel, and your auth
endpoint signs each player's joined_at, rating and criteria into
user_info (authorizeQueue() in @hibira-io/lobby/server), so players
cannot change their rating. The player who has waited longest hosts the
queue: it runs HibiraMatch.FindMatches and announces matches with
client-queue-match. The rules match findMatches() in
@hibira-io/lobby, so it makes no difference whether a Unity or a browser
player is hosting. Options and limits are in the
lobby-js README;
times here are in seconds.
Coming from Photon PUN 2? See Moving from Photon PUN 2.
HibiraAuctionHouse follows an auction house run by
@hibira-io/auction on your Node
server, so Unity and browser players trade in the same house. Listings,
bids, extensions and sales arrive live; the countdown follows the server's
clock.
var house = HibiraAuctionHouse.Open(hibira, "main", "https://api.your-game.example/auction");
house.Changed += () => DrawListings(house.Listings);
house.Updated += (kind, listing) =>
{
if (kind == "sold" && listing.BidderId == myId) ShowWon(listing.Item["type"]);
};
void Update()
{
foreach (var l in house.Listings) SetTimer(l.Id, house.TimeLeft(l)); // seconds
}The URL is your server's GET returning house.snapshot(); it loads on
every subscription, including after a reconnect. Bids never go over the
socket: POST them to your own server, which checks them with house.bid()
and publishes the result to everyone. listing.MinBid is the next lowest
bid, and house.Prices(type) the recent sale prices of an item type.
WebGL builds use the browser's WebSocket and fetch (through
UnityWebRequest), so two things change:
- Add the page's origin (for example
https://your-game.exampleor your itch.io page) to the app's allowed origins in the Hibira portal. - The auth endpoint needs CORS for that origin, or serve the game from the backend's own origin.
The browser answers the server's pings by itself, so a player who switches to another tab stays connected while Unity's frames are paused.
Everything runs on Unity's main thread: a hidden Hibira object ticks every
client once a frame. To tick a client yourself (in a test, say), set
ManualTick = true and call Tick().
HibiraClient
Connect(), Disconnect(), Dispose() |
open; close and stop reconnecting; close for good |
Subscribe(name), Unsubscribe(name), Channel(name) |
channels; subscriptions survive reconnects |
Trigger(channel, event, data) |
send a client event |
State, SocketId |
connection state and socket id |
StateChanged, Connected, Disconnected(code, reason, willReconnect), Error(code, message) |
connection events |
EventReceived |
every event on every channel |
HibiraChannel: Bind(event, handler), Unbind(event, handler),
Trigger(event, data), Unsubscribe(), IsSubscribed, Members, MyId,
and the events Subscribed, SubscriptionError, EventReceived,
MemberAdded, MemberRemoved, CountChanged.
HibiraQueue: Join(client, channel, options), Leave(), Status,
Position, Count, Tickets, Waited, Range, IsHost, Match, and the
events Joined, Updated, Matched, TimedOut, JoinFailed.
HibiraMatch: FindMatches(tickets, rules, nowMs), AllowedRange,
CriteriaFit.
HibiraAuctionHouse: Open(client, house, snapshotUrl) (or a load
function), Listings, Closed, Get(id), TimeLeft(listing),
Prices(type), NowMs(), Reload(), Close(), and the events Ready,
Changed, Updated(kind, listing), Failed. HibiraListing carries the
listing's fields (Price, MinBid, Buyout, EndsAt, Status, Item, …).
Not supported yet: end-to-end encrypted channels (private-encrypted-) and
user sign-in (pusher:signin).
Hibira's limits apply as for any client: client events are limited to 10 per second per connection, an event's data to 4 KB on the Free plan (10 KB on Hobby and Starter, more on higher plans; see pricing), and presence channels hold up to 100 members.
The C# in Runtime/ builds outside Unity too, which is how it is tested:
dotnet run --project Tests~/Hibira.Tests # protocol and room tests
dotnet build Tests~/UnityCompile # compiles against Unity 2021.3's assemblies
dotnet build Tests~/UnityCompile -p:WebGL=true # ... as a WebGL player build
(cd Tests~/webgl && npm ci && npm test) # the WebGL .jslib, in Node
python3 Tests~/check-meta.py # every asset has a .metaTests~/e2e plays a room between this client and an @hibira-io/lobby
player on a real server with client events turned on:
cd Tests~/e2e && npm ci
E2E_HOST=127.0.0.1 E2E_PORT=6001 E2E_TLS=0 E2E_APP_ID=... E2E_KEY=... E2E_SECRET=... npm test
# matchmaking: the C# queue hosts and matches a lobby-js player by rating
E2E_HOST=127.0.0.1 E2E_PORT=6001 E2E_TLS=0 E2E_APP_ID=... E2E_KEY=... E2E_SECRET=... npm run test:queueMIT.
