Sockets
Socket connections are used in Nakama to enable real-time communication and gameplay between servers and clients. Clients can establish a socket connection to the server, allowing the server to send data to and receive data from the client, enabling use of the real-time features in Nakama.
There are many different ways that socket connections can be used in Nakama, depending on the specific requirements of the game. Socket connections can be used in Nakama to enable:
- Tracking online status : Socket connections can be used to track which clients are online and available for gameplay. This allows you to keep track of which clients are active and which are inactive, and can help you manage resources effectively.
- Sending and receiving data: Socket connections are used to send and receive data between the game server and clients. This can include gameplay data, such as player movements and actions, as well as other types of data, such as chat messages and system messages.
Socket connections are a key tool for enabling real-time communication and gameplay in Nakama, and they are used for a variety of features, including chat , parties , multiplayer , and more.
Socket connection lifecycle #
Socket connections are established between the game server and clients, and they are used to send and receive data between the two. The lifecycle of a socket connection is as follows:
- The client establishes a socket connection
- The client sends and receives data to and from the server
- The client disconnects from the server
Establishing a socket connection #
A socket is created from the client object:
Sending and receiving data #
Once a socket connection has been established, the client can send and receive data to and from the server. This connection is used for all real-time features of Nakama. See specific examples on the pages for these features:
Realtime messages have a per-message size limit set by socket.max_message_size_bytes, 4 KB by default. A single socket message larger than this limit, such as a large RPC payload sent over the socket, causes the server to close the connection. For large payloads, send the RPC over HTTP instead, which uses the larger socket.max_request_size_bytes limit (256 KB by default), or split the data into smaller messages.
Disconnecting from the server #
Socket connections are closed when the client disconnects from the server. This can also be done manually by calling the given method on the socket:
When the server initiates the disconnect, it waits up to socket.close_ack_wait_ms (2 seconds by default) for the client to acknowledge the close before dropping the connection. A client that never acknowledges can make a server-initiated disconnect appear to hang until this timeout elapses.
The server can also close a connection on its own when the client stops answering the heartbeat, which usually means long blocking work on the client stalled its network loop. For the timings, the causes, and how to avoid it, see Socket heartbeat disconnects .
Socket events #
There are a number of events that can be used to monitor the state of a socket connection or events that occur on the socket, which can be used to trigger actions in your game.
The following events are available for socket connections:
| Event | Description |
|---|---|
closed | Received when a socket connection is closed. |
connected | Received when a socket connection is connected. |
ReceivedChannelMessage | Message received on a chat channel. |
ReceivedChannelPresence | A user has joined or left a chat channel. |
ReceivedError | An error has occurred on the socket. |
ReceivedMatchmakerMatched | Received a message indicating the matchmaker has found a match. |
ReceivedMatchState | Game state from a multiplayer match. |
ReceivedMatchPresence | A user has joined or left a multiplayer match. |
ReceivedNotification | The current user has received a notification. |
ReceivedParty | Received a party event. This occurs when the current user’s invitation request is accepted by the party leader of a closed party. |
ReceivedPartyClose | The current party has been closed. |
ReceivedPartyData | Received custom party data, such as party chat messages. |
ReceivedPartyJoinRequest | Request from a user to join a party. |
ReceivedPartyLeader | Indicates the party leader has been changed. |
ReceivedPartyMatchmakerTicket | Received a new matchmaker ticket for the party. |
ReceivedPartyPresence | A user has joined or left the party. |
ReceivedStatusPresence | Received a change to a user’s updated online status. |
ReceivedStreamPresence | A user has joined or left a realtime stream. |
ReceivedStreamState | Received a message from a realtime stream. |
