Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Nakama JS
https://github.com/heroiclabs/nakama-js
Admin
JavaScript client for Nakama server written in TypeScript.
Tokens:
59,476
Snippets:
663
Trust Score:
9.7
License:
Apache-2.0
Update:
1 month ago
Context
Chat
Benchmark
76.82
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Install @heroiclabs/nakama-js-protobuf via npm Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Optionally install the Protocol Buffers wire format adapter for use with sockets. ```bash npm install @heroiclabs/nakama-js-protobuf ``` -------------------------------- ### Install the adapter with yarn Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js-protobuf/README.md Adds the @heroiclabs/nakama-js-protobuf package to the project. ```shell yarn add "@heroiclabs/nakama-js-protobuf" ``` -------------------------------- ### Install @heroiclabs/nakama-js via npm Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Install the Nakama JavaScript client package from NPM. After installation, the code will be in the 'node_modules' folder and the package will be listed in 'package.json'. ```bash npm install @heroiclabs/nakama-js ``` -------------------------------- ### Install Satori JavaScript client via npm Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Install the @heroiclabs/satori-js package from NPM. After installation, the code appears in the node_modules folder and the package is listed in package.json. ```shell npm install @heroiclabs/satori-js ``` -------------------------------- ### Run tests with Docker Compose and npm test Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Start the server and database using Docker Compose, then run tests against each workspace bundle. Build the workspace first if source changes were made. ```bash docker-compose -f ./docker-compose.yml up npm run test --workspace=@heroiclabs/nakama-js-test ``` -------------------------------- ### Generate API docs with typedoc Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Install dependencies and run the docs script to generate API documentation with typedoc, which is deployed to GitHub pages. ```bash npm install && npm run docs ``` -------------------------------- ### Build satori-js package from source Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Install dependencies and build the satori-js workspace package. Requires Node v18 or later and Yarn. ```shell npm install --workspace=@heroiclabs/satori-js && npm run build --workspace=@heroiclabs/satori-js ``` -------------------------------- ### getSubscription(session, productId) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Gets a subscription by product ID. ```APIDOC ## getSubscription(session, productId) ### Description Gets a subscription by product ID. ### Method Client method ### Parameters - **session** (Session) - Required - The session object for authentication. - **productId** (string) - Required - The product ID of the subscription. ### Returns Promise<ApiValidatedSubscription> - A promise that resolves to the validated subscription. ### Source Defined in [nakama-js/client.ts:866](https://github.com/heroiclabs/nakama-js/blob/625e5b9/packages/nakama-js/client.ts#L866) ``` -------------------------------- ### getExperiments(session: Session, names?: string[]): Promise<ApiExperimentList> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Gets or lists all available experiments for this identity. ```APIDOC ## getExperiments(session: Session, names?: string[]): Promise<ApiExperimentList> ### Description Gets or lists all available experiments for this identity. ### Method getExperiments ### Parameters - **session** (Session) - Required - The session object for the authenticated user. - **names** (string[]) - Optional - A list of experiment names to filter by. ### Returns Promise<ApiExperimentList> - A promise that resolves to a list of experiments. ``` -------------------------------- ### onOpen Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the handler that is dispatched when the web socket opens. ```APIDOC ## onOpen ### Description Gets or sets the handler that is dispatched when the web socket opens. ### Property `onOpen` ### Type `null | SocketOpenHandler` ### Getter - `get onOpen(): null | SocketOpenHandler` - Returns the current handler. ### Setter - `set onOpen(value: null | SocketOpenHandler): void` - Sets the handler. ### Parameters (Setter) - **value** (`null | SocketOpenHandler`) - The new handler. ### Returns (Getter) - `null | SocketOpenHandler` ### Returns (Setter) - `void` ``` -------------------------------- ### onOpen Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the callback invoked when the WebSocket connection opens. ```APIDOC ## Accessor: onOpen ### Description Gets or sets the callback invoked when the WebSocket connection opens. ### Signature `get onOpen(): (evt: Event) => void` `set onOpen(value: (evt: Event) => void)` ### Parameters (setter) - **value** (function) - Required - The callback function to invoke on open. ### Returns (getter) - **function** - The current callback function. ``` -------------------------------- ### Build a specific workspace with npm run build --workspace Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Build a specific workspace in the monorepo by passing the --workspace flag to the build command. Requires Node v18 and npm install first. ```bash npm run build --workspace=@heroiclabs/nakama-js ``` -------------------------------- ### Get Account Details Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Fetches the account object for the current session, including user ID, username, and wallet. ```javascript const account = await client.getAccount(session); console.info(account.user.id); console.info(account.user.username); console.info(account.wallet); ``` -------------------------------- ### Create a Satori client with connection credentials Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Import the Client class and instantiate it with an API key, host, port, and SSL flag. Use useSSL=false for local development. ```js import {Client} from "@heroiclabs/satori-js"; const useSSL = false; const client = new Client("apiKey", "127.0.0.1", 7450, useSSL); ``` -------------------------------- ### new Client(apiKey?, host?, port?, useSSL?, timeout?, autoRefreshSession?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Creates a new Client instance with optional configuration parameters for connecting to the Satori server. ```APIDOC ## new Client(apiKey?, host?, port?, useSSL?, timeout?, autoRefreshSession?) ### Description Creates a new Client instance for interacting with the Satori server. ### Method Constructor ### Parameters - **apiKey** (string) - Optional - The API key for authentication. Defaults to DEFAULT_API_KEY. - **host** (string) - Optional - The server host. Defaults to DEFAULT_HOST. - **port** (string) - Optional - The server port. Defaults to DEFAULT_PORT. - **useSSL** (boolean) - Optional - Whether to use SSL. Defaults to false. - **timeout** (number) - Optional - The timeout in milliseconds. Defaults to DEFAULT_TIMEOUT_MS. - **autoRefreshSession** (boolean) - Optional - Whether to automatically refresh the session. Defaults to true. ### Returns - **Client** - A new Client instance. ``` -------------------------------- ### constructor Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Creates a new Client instance for the Satori server. ```APIDOC ## constructor ### Description Initializes a new Client instance with the provided configuration. ### Method constructor ### Parameters - **host** (string) - Required - The hostname of the Satori server. - **port** (number) - Required - The port of the Satori server. - **useSSL** (boolean) - Required - Whether to use SSL. - **timeout** (number) - Required - The request timeout in milliseconds. - **apiKey** (string) - Required - The API key for authentication. - **autoRefreshSession** (boolean) - Required - Whether to automatically refresh the session. - **expiredTimespanMs** (number) - Required - The timespan in milliseconds before session expiry to consider it expired. ### Request Example ```typescript const client = new Client({ host: 'localhost', port: 7450, useSSL: false, timeout: 5000, apiKey: 'your-api-key', autoRefreshSession: true, expiredTimespanMs: 60000 }); ``` ``` -------------------------------- ### getHeartbeatTimeoutMs Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Gets the heartbeat timeout in milliseconds. ```APIDOC ## getHeartbeatTimeoutMs ### Description Gets the heartbeat timeout in milliseconds. ### Method getHeartbeatTimeoutMs ### Parameters No parameters documented. ### Returns No return value documented. ``` -------------------------------- ### new Client(serverkey?, host?, port?, useSSL?, timeout?, autoRefreshSession?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Creates a new Client instance for interacting with the Nakama server. ```APIDOC ## new Client(serverkey?, host?, port?, useSSL?, timeout?, autoRefreshSession?) ### Description Creates a new Client instance for interacting with the Nakama server. ### Parameters - **serverkey** (string) - Optional - Default: `DEFAULT_SERVER_KEY` - **host** (string) - Optional - Default: `DEFAULT_HOST` - **port** (string) - Optional - Default: `DEFAULT_PORT` - **useSSL** (boolean) - Optional - Default: `false` - **timeout** (number) - Optional - Default: `DEFAULT_TIMEOUT_MS` - **autoRefreshSession** (boolean) - Optional - Default: `true` ### Returns - **Client** - A new Client instance. ``` -------------------------------- ### onError Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the callback invoked when an error occurs. ```APIDOC ## Accessor: onError ### Description Gets or sets the callback invoked when an error occurs. ### Signature `get onError(): (evt: Event) => void` `set onError(value: (evt: Event) => void)` ### Parameters (setter) - **value** (function) - Required - The callback function to invoke on error. ### Returns (getter) - **function** - The current callback function. ``` -------------------------------- ### Create a socket with the Protobuf adapter Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js-protobuf/README.md Imports the Client and WebSocketAdapterPb, then creates a client and socket with the adapter. The useSSL flag should be enabled if the server uses an SSL certificate. ```js import {Client} from "@heroiclabs/nakama-js"; import {WebSocketAdapterPb} from "@heroiclabs/nakama-js-protobuf" const useSSL = false; // Enable if server is run with an SSL certificate. const client = new Client("defaultkey", "127.0.0.1", 7350, useSSL); const trace = false; const socket = client.createSocket(useSSL, trace, new WebSocketAdapterPb()); ``` -------------------------------- ### onMessage Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the callback invoked when a message is received. ```APIDOC ## Accessor: onMessage ### Description Gets or sets the callback invoked when a message is received. ### Signature `get onMessage(): (evt: MessageEvent) => void` `set onMessage(value: (evt: MessageEvent) => void)` ### Parameters (setter) - **value** (function) - Required - The callback function to invoke on message. ### Returns (getter) - **function** - The current callback function. ``` -------------------------------- ### constructor Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Creates a new Client instance for connecting to a Nakama server. The constructor initializes the client with the provided configuration parameters. ```APIDOC ## constructor ### Description Creates a new Client instance for connecting to a Nakama server. ### Method constructor ### Signature `constructor(serverkey: string, host: string, port: number, useSSL: boolean, timeout: number, autoRefreshSession: boolean, expiredTimespanMs: number)` ### Parameters - **serverkey** (string) - Required - The server key for authentication. - **host** (string) - Required - The hostname of the Nakama server. - **port** (number) - Required - The port number of the Nakama server. - **useSSL** (boolean) - Required - Whether to use SSL/TLS. - **timeout** (number) - Required - The request timeout in milliseconds. - **autoRefreshSession** (boolean) - Required - Whether to automatically refresh the session. - **expiredTimespanMs** (number) - Required - The timespan in milliseconds before a session is considered expired. ### Returns A new Client instance. ``` -------------------------------- ### onClose Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the handler dispatched when the web socket closes. ```APIDOC ## onClose ### Description Gets or sets the handler dispatched when the web socket closes. ### Accessor Type Property accessor (getter/setter) ### Getter Signature `get onClose(): null | SocketCloseHandler` ### Getter Returns - `null | SocketCloseHandler` - The current close handler, or null if not set. ### Setter Signature `set onClose(value: null | SocketCloseHandler): void` ### Setter Parameters - `value` (null | SocketCloseHandler) - Required - The new close handler to set. ### Setter Returns - `void` ### Implementation Implements `WebSocketAdapter.onClose`. ``` -------------------------------- ### Use the WebSocket Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js-protobuf/README.md Sets up a disconnect handler and connects the socket with a session. ```js socket.ondisconnect = (evt) => { console.info("Disconnected", evt); }; const session = await socket.connect(session); // Socket is open. ``` -------------------------------- ### onClose Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the callback invoked when the WebSocket connection closes. ```APIDOC ## Accessor: onClose ### Description Gets or sets the callback invoked when the WebSocket connection closes. ### Signature `get onClose(): (evt: CloseEvent) => void` `set onClose(value: (evt: CloseEvent) => void)` ### Parameters (setter) - **value** (function) - Required - The callback function to invoke on close. ### Returns (getter) - **function** - The current callback function. ``` -------------------------------- ### Create a Nakama client object Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Import the Client class and instantiate it with connection credentials. The useSSL flag should be set to true if the server uses an SSL certificate. ```javascript import {Client} from "@heroiclabs/nakama-js"; var useSSL = false; // Enable if server is run with an SSL certificate. var client = new Client("defaultkey", "127.0.0.1", "7350", useSSL); ``` -------------------------------- ### connect(session: Session, createStatus: boolean): Promise<Session> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Connects to the server. ```APIDOC ## connect ### Description Connects to the server. ### Method connect ### Signature connect(session: Session, createStatus: boolean): Promise<Session> ### Parameters - **session** (Session) - Required - The session to connect with. - **createStatus** (boolean) - Required - Whether to create a status. ### Returns Promise<Session> ``` -------------------------------- ### onError Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the handler dispatched when the web socket receives an error. ```APIDOC ## onError ### Description Gets or sets the handler dispatched when the web socket receives an error. ### Accessor Type Property accessor (getter/setter) ### Getter Signature `get onError(): null | SocketErrorHandler` ### Getter Returns - `null | SocketErrorHandler` - The current error handler, or null if not set. ### Setter Signature `set onError(value: null | SocketErrorHandler): void` ### Setter Parameters - `value` (null | SocketErrorHandler) - Required - The new error handler to set. ### Setter Returns - `void` ### Implementation Implements `WebSocketAdapter.onError`. ``` -------------------------------- ### Create Socket and Connect Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Creates a socket with optional secure and trace flags, registers a disconnect listener, and connects using the session. ```javascript const secure = false; // Enable if server is run with an SSL certificate const trace = false; const socket = client.createSocket(secure, trace); socket.ondisconnect = (evt) => { console.info("Disconnected", evt); }; const session = await socket.connect(session); // Socket is open. ``` -------------------------------- ### onMessage Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Gets or sets the handler dispatched when the web socket receives a normal message. ```APIDOC ## onMessage ### Description Gets or sets the handler dispatched when the web socket receives a normal message. ### Accessor Type Property accessor (getter/setter) ### Getter Signature `get onMessage(): null | SocketMessageHandler` ### Getter Returns - `null | SocketMessageHandler` - The current message handler, or null if not set. ### Setter Signature `set onMessage(value: null | SocketMessageHandler): void` ### Setter Parameters - `value` (null | SocketMessageHandler) - Required - The new message handler to set. ### Setter Returns - `void` ### Implementation Implements `WebSocketAdapter.onMessage`. ``` -------------------------------- ### Authenticate and Connect with Nakama JS Client Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js-test/index.html Creates a Nakama client and socket, then authenticates with a custom ID and connects. Logs the session and any errors to the console. ```javascript const client = new nakamajs.Client(); const socket = client.createSocket(false, true); const customid = "someuniqueidentifier"; var _session; client.authenticateCustom(customid) .then(session => { _session = session; console.log("session is...") console.log(session); return socket.connect(session); }).then(session => { }).catch(error => { console.log("error is...") console.log(error); }); ``` -------------------------------- ### PartyMatchmakerTicket Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.PartyMatchmakerTicket.html Interface representing a response from starting a new party matchmaking process. It contains the party ID and the matchmaker ticket. ```APIDOC ## Interface PartyMatchmakerTicket ### Description A response from starting a new party matchmaking process. ### Properties #### party_id - **party_id** (string) - The ID of the party. #### ticket - **ticket** (string) - The matchmaker ticket created. ``` -------------------------------- ### connect(session: Session, createStatus?: boolean, connectTimeoutMs?: number): Promise<Session> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.DefaultSocket.html Connects to the server with the given session. ```APIDOC ## connect(session: Session, createStatus?: boolean, connectTimeoutMs?: number): Promise<Session> ### Description Connects to the server. ### Method connect ### Parameters - **session** (Session) - Required - The session to connect with. - **createStatus** (boolean) - Optional - Whether to create a status. Defaults to false. - **connectTimeoutMs** (number) - Optional - Connection timeout in milliseconds. Defaults to DefaultSocket.DefaultConnectTimeoutMs. ### Returns Promise<Session> ``` -------------------------------- ### getFlagDefault(name: string): Promise<never> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Gets a single flag with its configured default value. Throws an error when the flag does not exist. ```APIDOC ## getFlagDefault(name: string): Promise<never> ### Description Gets a single flag with its configured default value. Throws an error when the flag does not exist. ### Method getFlagDefault ### Parameters - **name** (string) - Required - The name of the flag to retrieve. ### Returns Promise<never> - This method always throws an error if the flag does not exist. ``` -------------------------------- ### Run tests for satori-js Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Run tests against each workspace bundle. Build the workspace first if source changes were made. ```shell npm run test --workspace=@heroiclabs/satori-js-test ``` -------------------------------- ### getFlag(session: Session, name: string): Promise<never> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Gets a single flag for this identity. Throws an error when the flag does not exist. ```APIDOC ## getFlag(session: Session, name: string): Promise<never> ### Description Gets a single flag for this identity. Throws an error when the flag does not exist. ### Method getFlag ### Parameters - **session** (Session) - Required - The session object for the authenticated user. - **name** (string) - Required - The name of the flag to retrieve. ### Returns Promise<never> - This method always throws an error if the flag does not exist. ``` -------------------------------- ### Get flags for a session Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Use the client's getFlags method with a session to retrieve feature flags. The result is logged to the console. ```js const flags = await client.getFlags(session); console.info("Flags:", flags); ``` -------------------------------- ### Release to NPM with build and publish Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Build the workspace and publish it to NPM with public access. Requires access to the @heroiclabs organization. ```bash npm run build --workspace=<workspace> && npm publish --access=public --workspace=<workspace> ``` -------------------------------- ### new DefaultSocket(host, port, useSSL?, verbose?, adapter?, sendTimeoutMs?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.DefaultSocket.html Creates a new DefaultSocket instance with the specified host, port, and optional settings. ```APIDOC ## new DefaultSocket(host, port, useSSL?, verbose?, adapter?, sendTimeoutMs?) ### Description Creates a new DefaultSocket instance for connecting to a Nakama server. ### Constructor `new DefaultSocket(host: string, port: string, useSSL?: boolean, verbose?: boolean, adapter?: WebSocketAdapter, sendTimeoutMs?: number): DefaultSocket` ### Parameters - **host** (string) - Required - The hostname of the Nakama server. - **port** (string) - Required - The port of the Nakama server. - **useSSL** (boolean) - Optional - Whether to use SSL (default: false). - **verbose** (boolean) - Optional - Enable verbose logging (default: false). - **adapter** (WebSocketAdapter) - Optional - Custom WebSocket adapter. - **sendTimeoutMs** (number) - Optional - Send timeout in milliseconds (default: DefaultSocket.DefaultSendTimeoutMs). ### Returns - **DefaultSocket** - A new DefaultSocket instance. ``` -------------------------------- ### connect Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Connects to the server. ```APIDOC ## connect ### Description Connects to the server. ### Method connect ### Parameters No parameters documented. ### Returns No return value documented. ``` -------------------------------- ### getFlagDefaultWithFallback(name: string, fallbackValue?: string): Promise<{ name: string; value: undefined | string; }> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Gets a single flag with its configured default value, or a fallback value if the flag is not found. ```APIDOC ## getFlagDefaultWithFallback(name: string, fallbackValue?: string): Promise<{ name: string; value: undefined | string; }> ### Description Gets a single flag with its configured default value, or a fallback value if the flag is not found. ### Method getFlagDefaultWithFallback ### Parameters - **name** (string) - Required - The name of the flag to retrieve. - **fallbackValue** (string) - Optional - The fallback value to use if the flag is not found. ### Returns Promise<{ name: string; value: undefined | string; }> - A promise that resolves to an object containing the flag name and its value (or undefined). ``` -------------------------------- ### Create Socket with Protobuf Adapter Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Creates a socket using the optional protocol buffer adapter from the nakama-js-protobuf package. ```javascript import {WebSocketAdapterPb} from "@heroiclabs/nakama-js-protobuf" const secure = false; // Enable if server is run with an SSL certificate const trace = false; const socket = client.createSocket(secure, trace, new WebSocketAdapterPb()); ``` -------------------------------- ### constructor Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Creates a new WebSocketAdapterText instance. ```APIDOC ## Constructor ### Description Creates a new WebSocketAdapterText instance. ### Signature `new WebSocketAdapterText(): WebSocketAdapterText` ### Returns - **WebSocketAdapterText** - A new instance of the WebSocketAdapterText class. ``` -------------------------------- ### connect Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Establishes a WebSocket connection to the specified URL. ```APIDOC ## Method: connect ### Description Establishes a WebSocket connection to the specified URL. ### Signature `connect(url: string): void` ### Parameters - **url** (string) - Required - The WebSocket URL to connect to. ### Returns - **void** - No return value. ``` -------------------------------- ### authenticate Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Authenticates a user with the Satori server. ```APIDOC ## authenticate ### Description Authenticates a user with the Satori server using the provided credentials. ### Method authenticate ### Parameters - **email** (string) - Required - The user's email. - **password** (string) - Required - The user's password. ### Request Example ```typescript const session = await client.authenticate('user@example.com', 'password123'); ``` ### Response Returns a Promise that resolves to a Session object. ``` -------------------------------- ### createParty(open: boolean, max_size: number): Promise<Party> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Creates a party. ```APIDOC ## createParty ### Description Creates a party. ### Method createParty ### Signature createParty(open: boolean, max_size: number): Promise<Party> ### Parameters - **open** (boolean) - Required - Whether the party is open. - **max_size** (number) - Required - The maximum size of the party. ### Returns Promise<Party> ``` -------------------------------- ### constructor Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.DefaultSocket.html Creates a new DefaultSocket instance. The constructor initializes the socket with the provided host, port, and options. ```APIDOC ## constructor ### Description Creates a new DefaultSocket instance. ### Signature `constructor(host?: string, port?: string, useSSL?: boolean, verbose?: boolean, sendTimeoutMs?: number, heartbeatTimeoutMs?: number, adapter?: WebSocketAdapter)` ### Parameters - **host** (string) - Optional - The hostname of the Nakama server. - **port** (string) - Optional - The port of the Nakama server. - **useSSL** (boolean) - Optional - Whether to use SSL/TLS. - **verbose** (boolean) - Optional - Enable verbose logging. - **sendTimeoutMs** (number) - Optional - Timeout for sending messages. - **heartbeatTimeoutMs** (number) - Optional - Timeout for heartbeats. - **adapter** (WebSocketAdapter) - Optional - Custom WebSocket adapter. ### Returns - A new DefaultSocket instance. ``` -------------------------------- ### createParty Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Creates a party. ```APIDOC ## createParty ### Description Creates a party. ### Method createParty ### Parameters No parameters documented. ### Returns No return value documented. ``` -------------------------------- ### authenticateDevice(id, create?, username?, vars?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Authenticate a user with a device id against the server. ```APIDOC ## authenticateDevice(id, create?, username?, vars?) ### Description Authenticate a user with a device id against the server. ### Method authenticateDevice ### Parameters - **id** (string) - Required - The device ID of the user. - **create** (boolean) - Optional - If true, create the user if they don't exist. - **username** (string) - Optional - The username for the user. - **vars** (Record<string, string>) - Optional - Additional variables. ### Returns Promise<Session> ``` -------------------------------- ### Create Socket with Protocol Buffer Adapter Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js/README.md Creates a socket using the optional protocol buffer adapter. The adapter is passed as the third argument to createSocket. ```javascript import {WebSocketAdapterPb} from "@heroiclabs/nakama-js-protobuf" const secure = false; // Enable if server is run with an SSL certificate const trace = false; const socket = client.createSocket(secure, trace, new WebSocketAdapterPb()); ``` -------------------------------- ### authenticateCustom(id, create?, username?, vars?, options?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Authenticate a user with a custom id against the server. ```APIDOC ## authenticateCustom(id, create?, username?, vars?, options?) ### Description Authenticate a user with a custom id against the server. ### Method authenticateCustom ### Parameters - **id** (string) - Required - The custom ID of the user. - **create** (boolean) - Optional - If true, create the user if they don't exist. - **username** (string) - Optional - The username for the user. - **vars** (Record<string, string>) - Optional - Additional variables. - **options** (any) - Optional - Additional options. ### Returns Promise<Session> ``` -------------------------------- ### createMatch(name?: string): Promise<Match> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Creates a multiplayer match on the server. ```APIDOC ## createMatch ### Description Creates a multiplayer match on the server. ### Method createMatch ### Signature createMatch(name?: string): Promise<Match> ### Parameters - **name** (string) - Optional - The name of the match. ### Returns Promise<Match> ``` -------------------------------- ### createSocket(useSSL?, verbose?, adapter?, sendTimeoutMs?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Creates a socket using the client's configuration. Allows optional SSL, verbosity, adapter, and send timeout settings. ```APIDOC ## createSocket(useSSL?, verbose?, adapter?, sendTimeoutMs?) ### Description A socket created with the client's configuration. ### Method Client method ### Parameters - **useSSL** (boolean) - Optional - Whether to use SSL (default: false). - **verbose** (boolean) - Optional - Enable verbose logging (default: false). - **adapter** (WebSocketAdapter) - Optional - Custom WebSocket adapter. - **sendTimeoutMs** (number) - Optional - Send timeout in milliseconds (default: DefaultSocket.DefaultSendTimeoutMs). ### Returns Socket - A Socket instance. ### Source Defined in [nakama-js/client.ts:762](https://github.com/heroiclabs/nakama-js/blob/625e5b9/packages/nakama-js/client.ts#L762) ``` -------------------------------- ### Generate Satori API client Source: https://github.com/heroiclabs/nakama-js/blob/master/openapi-gen/README.md Run this command to generate the Satori API client from the Swagger specification. The output is redirected to the TypeScript file api.gen.ts in the satori-js package. ```shell go run main.go "$GOPATH/src/github.com/heroiclabs/satori/api/satori.swagger.json" "Satori" > ../packages/satori-js/api.gen.ts ``` -------------------------------- ### new Session(token: string, refresh_token: string): Session Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Session.html Creates a new Session instance with the provided authentication token and refresh token. ```APIDOC ## Constructor: new Session(token: string, refresh_token: string) ### Description Creates a new Session instance with the provided authentication token and refresh token. ### Parameters - **token** (string) - Required - The authentication token. - **refresh_token** (string) - Required - The refresh token. ### Returns - **Session** - A new Session instance. ``` -------------------------------- ### getExperiments Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Retrieves experiments for the current identity. ```APIDOC ## getExperiments ### Description Retrieves experiments for the current identity. ### Method getExperiments ### Request Example ```typescript const experiments = await client.getExperiments(); ``` ### Response Returns a Promise that resolves to an array of Experiment objects. ``` -------------------------------- ### authenticateEmail(email, password, create?, username?, vars?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Authenticate a user with an email+password against the server. ```APIDOC ## authenticateEmail(email, password, create?, username?, vars?) ### Description Authenticate a user with an email+password against the server. ### Method authenticateEmail ### Parameters - **email** (string) - Required - The email address of the user. - **password** (string) - Required - The password of the user. - **create** (boolean) - Optional - If true, create the user if they don't exist. - **username** (string) - Optional - The username for the user. - **vars** (Record<string, string>) - Optional - Additional variables. ### Returns Promise<Session> ``` -------------------------------- ### new Session(token: string, refresh_token: string, created: boolean): Session Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Session.html Creates a new Session instance with the provided authentication token, refresh token, and creation flag. ```APIDOC ## new Session(token: string, refresh_token: string, created: boolean): Session ### Description Creates a new Session instance with the provided authentication token, refresh token, and creation flag. ### Method Constructor ### Parameters - **token** (string) - Required - The authentication token for the session. - **refresh_token** (string) - Required - The refresh token for the session. - **created** (boolean) - Required - Indicates whether the session was newly created. ### Returns - **Session** - A new Session instance. ``` -------------------------------- ### connect Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.WebSocketAdapterText.html Connects to the Nakama server using the provided connection parameters and authentication token. ```APIDOC ## connect ### Description Connects to the Nakama server using the provided connection parameters and authentication token. ### Method `connect(scheme: string, host: string, port: string, createStatus: boolean, token: string): void` ### Parameters - **scheme** (`string`) - The URL scheme (e.g., 'ws' or 'wss'). - **host** (`string`) - The server hostname. - **port** (`string`) - The server port. - **createStatus** (`boolean`) - Whether to create a status. - **token** (`string`) - The authentication token. ### Returns - `void` ``` -------------------------------- ### event Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Sends a single event to the Satori server. ```APIDOC ## event ### Description Sends a single event to the Satori server. ### Method event ### Parameters - **event** (Event) - Required - The event object to send. ### Request Example ```typescript await client.event({ name: 'purchase', metadata: { amount: 10 } }); ``` ``` -------------------------------- ### authenticateApple(token, create?, username?, vars?, options?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Authenticate a user with an Apple ID against the server. ```APIDOC ## authenticateApple(token, create?, username?, vars?, options?) ### Description Authenticate a user with an Apple ID against the server. ### Method authenticateApple ### Parameters - **token** (string) - Required - The Apple ID token. - **create** (boolean) - Optional - If true, create the user if they don't exist. - **username** (string) - Optional - The username for the user. - **vars** (Record<string, string>) - Optional - Additional variables. - **options** (any) - Optional - Additional options. ### Returns Promise<Session> ``` -------------------------------- ### Authenticate with Satori Client using Custom ID Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js-test/index.html Initializes a Satori client with a specific API key and authenticates a user using a custom ID. The session is stored in a variable and logged on success; errors are logged to the console. ```javascript const client = new satorijs.Client("a63e9083-375c-4169-ad0b-dccb86bdb98b"); const customid = "uniqueuser"; var _session; client.authenticate(customid) .then(session => { _session = session; console.log("session", session); }) .catch(error => { console.log(error); }); ``` -------------------------------- ### Authenticate with Email and Password Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/nakama-js/README.md Authenticates a user with an email and password. If the user does not exist, it may be created. The returned session is used for subsequent requests. ```javascript var email = "super@heroes.com"; var password = "batsignal"; const session = await client.authenticateEmail(email, password); console.info(session); ``` -------------------------------- ### addMatchmakerParty(party_id: string, query: string, min_count: number, max_count: number, string_properties?: Record<string, string>, numericProperties?: Record<string, number>): Promise<PartyMatchmakerTicket> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Begins matchmaking as a party. ```APIDOC ## addMatchmakerParty ### Description Begins matchmaking as a party. ### Method addMatchmakerParty ### Signature addMatchmakerParty(party_id: string, query: string, min_count: number, max_count: number, string_properties?: Record<string, string>, numericProperties?: Record<string, number>): Promise<PartyMatchmakerTicket> ### Parameters - **party_id** (string) - Required - The ID of the party. - **query** (string) - Required - The matchmaker query. - **min_count** (number) - Required - The minimum number of players to match. - **max_count** (number) - Required - The maximum number of players to match. - **string_properties** (Record<string, string>) - Optional - String properties for matchmaking. - **numericProperties** (Record<string, number>) - Optional - Numeric properties for matchmaking. ### Returns Promise<PartyMatchmakerTicket> ``` -------------------------------- ### Properties Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html The Client class exposes several properties that configure the connection and session behavior. These properties are set during construction and can be accessed directly. ```APIDOC ## Properties ### Description Configuration properties for the Client instance. ### Properties - **apiClient** - The underlying API client used for HTTP requests. - **autoRefreshSession** (boolean) - Whether the session is automatically refreshed. - **expiredTimespanMs** (number) - The timespan in milliseconds before a session is considered expired. - **host** (string) - The hostname of the Nakama server. - **port** (number) - The port number of the Nakama server. - **serverkey** (string) - The server key used for authentication. - **timeout** (number) - The request timeout in milliseconds. - **useSSL** (boolean) - Whether SSL/TLS is used. ``` -------------------------------- ### addMatchmakerParty(party_id: string, query: string, min_count: number, max_count: number, string_properties?: Record<string, string>, numeric_properties?: Record<string, number>): Promise<PartyMatchmakerTicket> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.DefaultSocket.html Begins matchmaking as a party. ```APIDOC ## addMatchmakerParty(party_id: string, query: string, min_count: number, max_count: number, string_properties?: Record<string, string>, numeric_properties?: Record<string, number>): Promise<PartyMatchmakerTicket> ### Description Begins matchmaking as a party. ### Method addMatchmakerParty ### Parameters - **party_id** (string) - Required - The ID of the party. - **query** (string) - Required - The matchmaker query. - **min_count** (number) - Required - The minimum number of players. - **max_count** (number) - Required - The maximum number of players. - **string_properties** (Record<string, string>) - Optional - String properties for matchmaking. - **numeric_properties** (Record<string, number>) - Optional - Numeric properties for matchmaking. ### Returns Promise<PartyMatchmakerTicket> ``` -------------------------------- ### events Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Sends multiple events to the Satori server. ```APIDOC ## events ### Description Sends multiple events to the Satori server. ### Method events ### Parameters - **events** (Event[]) - Required - An array of event objects to send. ### Request Example ```typescript await client.events([{ name: 'purchase' }, { name: 'level_up' }]); ``` ``` -------------------------------- ### linkFacebookInstantGame(session, request): Promise<boolean> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Adds Facebook Instant to the social profiles on the current user's account. ```APIDOC ## linkFacebookInstantGame(session, request): Promise<boolean> ### Description Add Facebook Instant to the social profiles on the current user's account. ### Method linkFacebookInstantGame ### Parameters - **session** (Session) - Required - The current user's session. - **request** (ApiAccountFacebookInstantGame) - Required - The Facebook Instant account information to link. ### Returns Promise<boolean> - A promise that resolves to true if the operation succeeds. ``` -------------------------------- ### listTournaments(session, categoryStart?, categoryEnd?, startTime?, endTime?, limit?, cursor?) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Lists current or upcoming tournaments with optional filters for category, time range, limit, and cursor. ```APIDOC ## listTournaments ### Description List current or upcoming tournaments. ### Method listTournaments ### Signature listTournaments(session: Session, categoryStart?: number, categoryEnd?: number, startTime?: number, endTime?: number, limit?: number, cursor?: string): Promise<TournamentList> ### Parameters - **session** (Session) - Required - The session object. - **categoryStart** (number) - Optional - Start of category range. - **categoryEnd** (number) - Optional - End of category range. - **startTime** (number) - Optional - Start time filter. - **endTime** (number) - Optional - End time filter. - **limit** (number) - Optional - Maximum number of tournaments to return. - **cursor** (string) - Optional - Cursor for pagination. ### Returns Promise<TournamentList> - A promise that resolves to a TournamentList. ### Source Defined in [nakama-js/client.ts:1536](https://github.com/heroiclabs/nakama-js/blob/625e5b9/packages/nakama-js/client.ts#L1536) ``` -------------------------------- ### identify Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/satori_js.Client.html Identifies a user with the Satori server. ```APIDOC ## identify ### Description Identifies a user with the Satori server. ### Method identify ### Parameters - **id** (string) - Required - The user ID. - **properties** (object) - Optional - Additional properties. ### Request Example ```typescript await client.identify('user-id', { tier: 'premium' }); ``` ``` -------------------------------- ### Generate Nakama API client Source: https://github.com/heroiclabs/nakama-js/blob/master/openapi-gen/README.md Run this command to generate the Nakama API client from the Swagger specification. The output is redirected to the TypeScript file api.gen.ts in the nakama-js package. ```shell go run main.go "$GOPATH/src/github.com/heroiclabs/nakama/apigrpc/apigrpc.swagger.json" "Nakama" > ../packages/nakama-js/api.gen.ts ``` -------------------------------- ### Update protocol buffer web socket adapter with npx protoc Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Regenerate the TypeScript required for the protocol buffer adapter. Run from packages/nakama-js-protobuf with the specified plugin and proto paths. ```bash npx protoc \ --plugin="./node_modules/.bin/protoc-gen-ts_proto" \ --proto_path=$GOPATH/src/github.com/heroiclabs/nakama-common \ --ts_proto_out=. \ --ts_proto_opt=snakeToCamel=false \ --ts_proto_opt=esModuleInterop=true \ $GOPATH/src/github.com/heroiclabs/nakama-common/rtapi/realtime.proto \ $GOPATH/src/github.com/heroiclabs/nakama-common/api/api.proto ``` -------------------------------- ### rpc(session, id, input) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Executes an RPC function on the server using the provided session, RPC ID, and input object. ```APIDOC ## rpc(session: Session, id: string, input: object): Promise<RpcResponse> ### Description Executes an RPC function on the server. ### Method rpc ### Parameters - **session** (Session) - Required - The session object. - **id** (string) - Required - The ID of the RPC function to execute. - **input** (object) - Required - The input object to pass to the RPC function. ### Returns Promise<RpcResponse> - A promise that resolves to the RPC response. ### Source Defined in [nakama-js/client.ts:1742](https://github.com/heroiclabs/nakama-js/blob/625e5b9/packages/nakama-js/client.ts#L1742) ``` -------------------------------- ### Authenticate with Email Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/index.html Authenticates a user with email and password, creating the user if they don't exist. The session object is returned and logged. ```javascript var email = "super@heroes.com";var password = "batsignal";const session = await client.authenticateEmail(email, password);console.info(session); ``` -------------------------------- ### Authenticate a user with Satori Source: https://github.com/heroiclabs/nakama-js/blob/master/packages/satori-js/README.md Authenticate by providing a unique user ID. The returned session is stored and logged; errors are caught and logged. ```js const userId = "<UniqueUserId>"; client.authenticate(userId) .then(session => { _session = session; console.info("Authenticated:", session); }).catch(error => { console.error("Error:", error); }); ``` -------------------------------- ### addMatchmaker(query: string, minCount: number, maxCount: number, stringProperties?: Record<string, string>, numericProperties?: Record<string, number>): Promise<MatchmakerTicket> Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/interfaces/nakama_js.Socket.html Joins the matchmaker pool and searches for opponents on the server. ```APIDOC ## addMatchmaker ### Description Joins the matchmaker pool and searches for opponents on the server. ### Method addMatchmaker ### Signature addMatchmaker(query: string, minCount: number, maxCount: number, stringProperties?: Record<string, string>, numericProperties?: Record<string, number>): Promise<MatchmakerTicket> ### Parameters - **query** (string) - Required - The matchmaker query. - **minCount** (number) - Required - The minimum number of players to match. - **maxCount** (number) - Required - The maximum number of players to match. - **stringProperties** (Record<string, string>) - Optional - String properties for matchmaking. - **numericProperties** (Record<string, number>) - Optional - Numeric properties for matchmaking. ### Returns Promise<MatchmakerTicket> ``` -------------------------------- ### writeTournamentRecord(session, tournamentId, request) Source: https://github.com/heroiclabs/nakama-js/blob/master/docs/classes/nakama_js.Client.html Writes a record to a tournament. Takes a session, tournament ID, and a WriteTournamentRecord request object, returning a Promise with a LeaderboardRecord. ```APIDOC ## writeTournamentRecord(session, tournamentId, request) ### Description Writes a record to a tournament. ### Method writeTournamentRecord ### Parameters - **session** (Session) - Required - The session object for authentication. - **tournamentId** (string) - Required - The identifier of the tournament. - **request** (WriteTournamentRecord) - Required - The record data to write. ### Returns Promise<LeaderboardRecord> - A promise that resolves to the written tournament record. ### Defined in [nakama-js/client.ts:2045](https://github.com/heroiclabs/nakama-js/blob/625e5b9/packages/nakama-js/client.ts#L2045) ```