The Satori Client connects to a Satori Server and is the entry point to access Satori features. It is recommended to have one client per server per game.
To create a client pass in your server connection details:
Authenticate users using the Satori Client via their unique ID.
When authenticating, you can optionally pass in any desired default_properties and/or custom_properties to be updated. If none are provided, the properties remain as they are on the server.
When authenticated the server responds with an auth token (JWT) which gets deserialized into a session table containing the authentication token, a refresh token and timestamps when the tokens expire.
Sessions expire after five (5) minutes by default. Expiring inactive sessions is good security practice.
Satori provides ways to restore sessions, for example when players re-launch the game, or refresh tokens to keep the session active while the game is being played.
Use the auth and refresh tokens on the session object to restore or refresh sessions.
localsession=require"nakama.session"localfunctionauthenticate()localjwt=session.restore("satori")-- refresh if the stored session token is about to expire!ifjwtandsession.is_token_expired_soon(jwt)andnotsession.is_refresh_token_expired(jwt)thenprint("Session has expired or is about to expire. Refreshing.")jwt=client.session_refresh(jwt.refresh_token)end-- login again if no session token is stored or it has expiredifnotjwtorsession.is_refresh_token_expired(jwt)thenprint("Session does not exist or it has expired. Must reauthenticate.")localid=uuid()localcustom_properties=nillocaldefault_properties=niljwt=client.authenticate(custom_properties,default_properties,id)end-- at this point we either have a valid session or no session at allifjwtthenprint("Session is valid. User is authenticated.")client.set_bearer_token(jwt.token)session.store(jwt,"satori")returntrueendprint("No valid session. Authentication failed.")returnfalseend
If you want to submit events to Satori before a user has authenticated with the game server backend (e.g. Nakama) and has a User ID, you should authenticate with Satori using a temporary ID, such as the device’s unique identifier or a randomly generated one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- If the user's device ID is already stored, grab thatlocaluuid=require"nakama.util.uuid"localplayer_prefs=sys.load("mygame","player_prefs")-- If the device identifier is invalid then let's generate a unique one.ifnotplayer_prefs.device_idthenplayer_prefs.device_id=uuid()-- Save the user's device ID so it can be retrieved during a later play session for re-authenticating.sys.save("mygame","player_prefs",player_prefs)end-- Authenticate with Satorilocalsatori_session=client.authenticate(nil,nil,player_prefs.device_id)ifsatori_session.tokenthenprint("Authenticated with Satori")elseprint("Error authenticating")end
You can then submit events before the user has authenticated with the game backend.
Once a user has successfully authenticated, you should then call identify to enrich the current session and return a new session that should be used for submitting future events.
Satori integrates with various messaging services to send messages to users via messages schedules. You can list a user’s messages, update a message’s status, and delete a message.