View as Markdown

Using Hooks

Hooks are a feature of the Nakama server runtime that allow you to run custom server code before or after certain server events happen. This can be useful in a number of scenarios, some of which are given below as examples.

All registration calls below are to be run inside the InitModule function.

For more information on hooks and how they can be used see the hooks documentation.

Message names #

Provided here is a full list of server messages that can benefit from hooks.

If your runtime code is in Go, refer to the interface definition for a full list of hooks that are available in the runtime package.

Use the following request names for registering your Before and After hooks:

Request NameDescription
AddFriendsAdd friends by ID or username to a user’s account.
AddGroupUsersAdd users to a group.
AuthenticateAppleAuthenticate a user with Apple Sign-In against the server.
AuthenticateCustomAuthenticate a user with a custom id against the server.
AuthenticateDeviceAuthenticate a user with a device id against the server.
AuthenticateEmailAuthenticate a user with an email+password against the server.
AuthenticateFacebookAuthenticate a user with a Facebook OAuth token against the server.
AuthenticateFacebookInstantGameAuthenticate a user with a Facebook Instant Game against the server.
AuthenticateGameCenterAuthenticate a user with Apple’s GameCenter against the server.
AuthenticateGoogleAuthenticate a user with Google against the server.
AuthenticateSteamAuthenticate a user with Steam against the server.
BanGroupUsersBan a set of users from a group.
BlockFriendsBlock one or more users by ID or username.
CreateGroupCreate a new group with the current user as the owner.
DeleteAccountDelete the current user’s account.
DeleteFriendsDelete one or more users by ID or username.
DeleteGroupDelete one or more groups by ID.
DeleteLeaderboardRecordDelete a leaderboard record.
DeleteNotificationsDelete one or more notifications for the current user.
DeleteStorageObjectsDelete one or more objects by ID or username.
DeleteTournamentRecordDelete a tournament record.
DemoteGroupUsersDemote a set of users in a group to a lower role.
GetAccountFetch the current user’s account.
GetMatchmakerStatsGet matchmaker queue statistics.
GetSubscriptionGet a validated subscription for a user.
GetUsersFetch zero or more users by ID and/or username.
HealthcheckA healthcheck which load balancers can use to check the service.
ImportFacebookFriendsImport Facebook friends and add them to a user’s account.
ImportSteamFriendsImport Steam friends and add them to a user’s account.
JoinGroupImmediately join an open group, or request to join a closed one.
JoinTournamentAttempt to join an open tournament.
KickGroupUsersKick a set of users from a group.
LeaveGroupLeave a group the user is a member of.
LinkAppleAdd Apple Sign-In to the social profiles on the current user’s account.
LinkCustomAdd a custom ID to the social profiles on the current user’s account.
LinkDeviceAdd a device ID to the social profiles on the current user’s account.
LinkEmailAdd an email+password to the social profiles on the current user’s account.
LinkFacebookAdd Facebook to the social profiles on the current user’s account.
LinkFacebookInstantGameAdd a Facebook Instant Game to the social profiles on the current user’s account.
LinkGameCenterAdd Apple’s GameCenter to the social profiles on the current user’s account.
LinkGoogleAdd Google to the social profiles on the current user’s account.
LinkSteamAdd Steam to the social profiles on the current user’s account.
ListChannelMessagesList a channel’s message history.
ListFriendsList all friends for the current user.
ListFriendsOfFriendsList friends of the current user’s friends.
ListGroupsList groups based on given filters.
ListGroupUsersList all users that are part of a group.
ListLeaderboardRecordsList leaderboard records.
ListLeaderboardRecordsAroundOwnerList leaderboard records around a given owner.
ListMatchesFetch a list of running matches.
ListNotificationsFetch a list of notifications.
ListPartiesList parties.
ListStorageObjectsList publicly readable storage objects in a given collection.
ListSubscriptionsList validated subscriptions for a user.
ListTournamentRecordsList tournament records.
ListTournamentRecordsAroundOwnerList tournament records around a given owner.
ListTournamentsList tournaments.
ListUserGroupsList groups the current user belongs to.
PromoteGroupUsersPromote a set of users in a group to the next role up.
ReadStorageObjectsGet storage objects.
SessionLogoutLog out a session and optionally invalidate its tokens.
SessionRefreshRefresh a user’s session using a refresh token.
UnlinkAppleRemove Apple Sign-In from the social profiles on the current user’s account.
UnlinkCustomRemove the custom ID from the social profiles on the current user’s account.
UnlinkDeviceRemove the device ID from the social profiles on the current user’s account.
UnlinkEmailRemove the email+password from the social profiles on the current user’s account.
UnlinkFacebookRemove Facebook from the social profiles on the current user’s account.
UnlinkFacebookInstantGameRemove a Facebook Instant Game from the social profiles on the current user’s account.
UnlinkGameCenterRemove Apple’s GameCenter from the social profiles on the current user’s account.
UnlinkGoogleRemove Google from the social profiles on the current user’s account.
UnlinkSteamRemove Steam from the social profiles on the current user’s account.
UpdateAccountUpdate fields in the current user’s account.
UpdateGroupUpdate fields in a given group.
ValidatePurchaseAppleValidate and record an Apple IAP receipt.
ValidatePurchaseFacebookInstantValidate and record a Facebook Instant Game IAP receipt.
ValidatePurchaseGoogleValidate and record a Google IAP receipt.
ValidatePurchaseHuaweiValidate and record a Huawei IAP receipt.
ValidateSubscriptionAppleValidate and record an Apple subscription receipt.
ValidateSubscriptionGoogleValidate and record a Google subscription receipt.
WriteLeaderboardRecordWrite a record to a leaderboard.
WriteStorageObjectsWrite objects into the storage engine.
WriteTournamentRecordWrite a record to a tournament.

Names are case-insensitive. For more information, see apigrpc.proto.

For real-time before and after hooks, use the following message names:

Message NameDescription
ChannelJoinJoin a realtime chat channel.
ChannelLeaveLeave a realtime chat channel.
ChannelMessageSendSend a message to a realtime chat channel.
ChannelMessageUpdateUpdate a message previously sent to a realtime chat channel.
ChannelMessageRemoveRemove a message previously sent to a realtime chat channel.
MatchCreateA client to server request to create a realtime match.
MatchDataSendA client to server request to send data to a realtime match.
MatchJoinA client to server request to join a realtime match.
MatchLeaveA client to server request to leave a realtime match.
MatchmakerAddSubmit a new matchmaking process request.
MatchmakerRemoveCancel a matchmaking process using a ticket.
PartyCreateA client to server request to create a party.
PartyJoinA client to server request to join a party.
PartyLeaveA client to server request to leave a party.
PartyPromoteA client to server request to promote a party member.
PartyAcceptA client to server request to accept a party join request.
PartyRemoveA client to server request to remove a party member.
PartyCloseA client to server request to close a party.
PartyJoinRequestListA client to server request to list all join requests for a particular party.
PartyMatchmakerAddSubmit a new party matchmaking process request.
PartyMatchmakerRemoveCancel a party matchmaking process using a ticket.
PartyDataSendA client to server request to send data to a party.
StatusFollowStart following some set of users to receive their status updates.
StatusUnfollowStop following some set of users to no longer receive their status updates.
StatusUpdateSet the user’s own status.

Names are case-insensitive. For more information, have a look at realtime.proto.

Before hooks #

These hooks run before a particular server runtime event occurs. This can be used to modify the input to a particular function or take additional actions before it occurs.

Before creating a group #

This hook checks that a group name does not contain profanity before allowing it to be created. Note that profanity checking functionality is not provided by Nakama.

Before deleting a group #

This hook denies an attempt to delete a group that still contains more than 1 user.

After hooks #

These hooks run after a particular server runtime event occurs. This can be used to respond to an event appropriately after it has happened.

After adding friends #

This hook sends a notification to each user that has been added as a friend by another user, letting them know that they have received a friend request.

After leaving a group #

This hook sends a message to a group notifying the group users that a particular user has left.

After authenticating by device ID #

This hook rewards a player with 10 coins (in their virtual wallet whenever they authenticate by device ID.

Event hooks #

These hooks run after a particular event has happened, such as a leaderboard being reset or a tournament ending.

On leaderboard reset #

This hook rewards the top 3 scoring players in a leaderboard with 1,000 coins whenever the leaderboard resets.

On tournament end #

This hook rewards the winner of a tournament with 10,000 coins and gives them a rare item when the tournament ends.

Before realtime hooks #

These hooks run before a specific realtime message is processed by the server.

Before channel join #

This hook checks to see if a user is trying to join a Direct Message channel for a non-friend. If so, it blocks their request to join the channel.

After realtime hooks #

These hooks run after a specific realtime message is processed by the server.

After channel leave #

This hook lets a recipient know that the other user has left the channel after that user has left.