SearchGameSessions - Amazon GameLift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

SearchGameSessions

Retrieves all active game sessions that match a set of search criteria and sorts them into a specified order.

This operation is not designed to continually track game session status because that practice can cause you to exceed your API limit and generate errors. Instead, configure an Amazon Simple Notification Service (Amazon SNS) topic to receive notifications from a matchmaker or a game session placement queue.

When searching for game sessions, you specify exactly where you want to search and provide a search filter expression, a sort expression, or both. A search request can search only one fleet, but it can search all of a fleet's locations.

This operation can be used in the following ways:

  • To search all game sessions that are currently running on all locations in a fleet, provide a fleet or alias ID. This approach returns game sessions in the fleet's home Region and all remote locations that fit the search criteria.

  • To search all game sessions that are currently running on a specific fleet location, provide a fleet or alias ID and a location name. For location, you can specify a fleet's home Region or any remote location.

Use the pagination parameters to retrieve results as a set of sequential pages.

If successful, a GameSession object is returned for each game session that matches the request. Search finds game sessions that are in ACTIVE status only. To retrieve information on game sessions in other statuses, use DescribeGameSessions .

To set search and sort criteria, create a filter expression using the following game session attributes. For game session search examples, see the Examples section of this topic.

  • gameSessionId -- A unique identifier for the game session. You can use either a GameSessionId or GameSessionArn value.

  • gameSessionName -- Name assigned to a game session. Game session names do not need to be unique to a game session.

  • gameSessionProperties -- A set of key-value pairs that can store custom data in a game session. For example: {"Key": "difficulty", "Value": "novice"}. The filter expression must specify the GameProperty -- a Key and a string Value to search for the game sessions.

    For example, to search for the above key-value pair, specify the following search filter: gameSessionProperties.difficulty = "novice". All game property values are searched as strings.

    For examples of searching game sessions, see the ones below, and also see Search game sessions by game property.

  • maximumSessions -- Maximum number of player sessions allowed for a game session.

  • creationTimeMillis -- Value indicating when a game session was created. It is expressed in Unix time as milliseconds.

  • playerSessionCount -- Number of players currently connected to a game session. This value changes rapidly as players join the session or drop out.

  • hasAvailablePlayerSessions -- Boolean value indicating whether a game session has reached its maximum number of players. It is highly recommended that all search requests include this filter attribute to optimize search performance and return only sessions that players can join.

Note

Returned values for playerSessionCount and hasAvailablePlayerSessions change quickly as players join sessions and others drop out. Results should be considered a snapshot in time. Be sure to refresh search results often, and handle sessions that fill up before a player can join.

All APIs by task

Request Syntax

{ "AliasId": "string", "FilterExpression": "string", "FleetId": "string", "Limit": number, "Location": "string", "NextToken": "string", "SortExpression": "string" }

Request Parameters

For information about the parameters that are common to all actions, see Common Parameters.

The request accepts the following data in JSON format.

Note

In the following list, the required parameters are described first.

AliasId

A unique identifier for the alias associated with the fleet to search for active game sessions. You can use either the alias ID or ARN value. Each request must reference either a fleet ID or alias ID, but not both.

Type: String

Pattern: ^alias-\S+|^arn:.*:alias\/alias-\S+

Required: No

FilterExpression

String containing the search criteria for the session search. If no filter expression is included, the request returns results for all game sessions in the fleet that are in ACTIVE status.

A filter expression can contain one or multiple conditions. Each condition consists of the following:

  • Operand -- Name of a game session attribute. Valid values are gameSessionName, gameSessionId, gameSessionProperties, maximumSessions, creationTimeMillis, playerSessionCount, hasAvailablePlayerSessions.

  • Comparator -- Valid comparators are: =, <>, <, >, <=, >=.

  • Value -- Value to be searched for. Values may be numbers, boolean values (true/false) or strings depending on the operand. String values are case sensitive and must be enclosed in single quotes. Special characters must be escaped. Boolean and string values can only be used with the comparators = and <>. For example, the following filter expression searches on gameSessionName: "FilterExpression": "gameSessionName = 'Matt\\'s Awesome Game 1'".

To chain multiple conditions in a single expression, use the logical keywords AND, OR, and NOT and parentheses as needed. For example: x AND y AND NOT z, NOT (x OR y).

Session search evaluates conditions from left to right using the following precedence rules:

  1. =, <>, <, >, <=, >=

  2. Parentheses

  3. NOT

  4. AND

  5. OR

For example, this filter expression retrieves game sessions hosting at least ten players that have an open player slot: "maximumSessions>=10 AND hasAvailablePlayerSessions=true".

Type: String

Length Constraints: Minimum length of 1. Maximum length of 1024.

Required: No

FleetId

A unique identifier for the fleet to search for active game sessions. You can use either the fleet ID or ARN value. Each request must reference either a fleet ID or alias ID, but not both.

Type: String

Pattern: ^fleet-\S+|^arn:.*:fleet\/fleet-\S+

Required: No

Limit

The maximum number of results to return. Use this parameter with NextToken to get results as a set of sequential pages. The maximum number of results returned is 20, even if this value is not set or is set higher than 20.

Type: Integer

Valid Range: Minimum value of 1.

Required: No

Location

A fleet location to search for game sessions. You can specify a fleet's home Region or a remote location. Use the Amazon Region code format, such as us-west-2.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 64.

Pattern: ^[A-Za-z0-9\-]+

Required: No

NextToken

A token that indicates the start of the next sequential page of results. Use the token that is returned with a previous call to this operation. To start at the beginning of the result set, do not specify a value.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 1024.

Required: No

SortExpression

Instructions on how to sort the search results. If no sort expression is included, the request returns results in random order. A sort expression consists of the following elements:

  • Operand -- Name of a game session attribute. Valid values are gameSessionName, gameSessionId, gameSessionProperties, maximumSessions, creationTimeMillis, playerSessionCount, hasAvailablePlayerSessions.

  • Order -- Valid sort orders are ASC (ascending) and DESC (descending).

For example, this sort expression returns the oldest active sessions first: "SortExpression": "creationTimeMillis ASC". Results with a null value for the sort operand are returned at the end of the list.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 1024.

Required: No

Response Syntax

{ "GameSessions": [ { "CreationTime": number, "CreatorId": "string", "CurrentPlayerSessionCount": number, "DnsName": "string", "FleetArn": "string", "FleetId": "string", "GameProperties": [ { "Key": "string", "Value": "string" } ], "GameSessionData": "string", "GameSessionId": "string", "IpAddress": "string", "Location": "string", "MatchmakerData": "string", "MaximumPlayerSessionCount": number, "Name": "string", "PlayerSessionCreationPolicy": "string", "Port": number, "Status": "string", "StatusReason": "string", "TerminationTime": number } ], "NextToken": "string" }

Response Elements

If the action is successful, the service sends back an HTTP 200 response.

The following data is returned in JSON format by the service.

GameSessions

A collection of objects containing game session properties for each session that matches the request.

Type: Array of GameSession objects

NextToken

A token that indicates where to resume retrieving results on the next call to this operation. If no token is returned, these results represent the end of the list.

Type: String

Length Constraints: Minimum length of 1. Maximum length of 1024.

Errors

For information about the errors that are common to all actions, see Common Errors.

InternalServiceException

The service encountered an unrecoverable internal failure while processing the request. Clients can retry such requests immediately or after a waiting period.

HTTP Status Code: 500

InvalidRequestException

One or more parameter values in the request are invalid. Correct the invalid parameter values before retrying.

HTTP Status Code: 400

NotFoundException

The requested resources was not found. The resource was either not created yet or deleted.

HTTP Status Code: 400

TerminalRoutingStrategyException

The service is unable to resolve the routing for a particular alias because it has a terminal RoutingStrategy associated with it. The message returned in this exception is the message defined in the routing strategy itself. Such requests should only be retried if the routing strategy for the specified alias is modified.

HTTP Status Code: 400

UnauthorizedException

The client failed authentication. Clients should not retry such requests.

HTTP Status Code: 400

UnsupportedRegionException

The requested operation is not supported in the Region specified.

HTTP Status Code: 400

Examples

Search game sessions

In this example, we want to find all game sessions that have at least two players already connected. We also want to filter out active game sessions that are not accepting new players.

This example illustrates a search that includes all of the fleet's locations. The results include a matching game session in the fleet's home Region (us-west-2) and another in a remote location (ca-central-1).

Sample Request

{"AliasId": "MOG-base", "FilterExpression": "playerSessionCount>=2 AND hasAvailablePlayerSessions=true", "Limit": 2 } CLI syntax: aws gamelift search-game-sessions --alias-id "MOG-base" --filter-expression "playerSessionCount>=2 AND hasAvailablePlayerSessions=true" --limit 2

Sample Response

{ "GameSessions": [ { "CreationTime": 1469498468.057, "CurrentPlayerSessionCount": 5, "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "easy"}, {"Key": "gameMap","Value": "Snowfall"}, {"Key": "gameMode","Value": "Explore"} ], "GameSessionId": "gsess-4444dddd-55ee-66ff-77aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 10, "Name": "Matt's Awesome Game win123", "Port": "8080", "Status": "ACTIVE", "Location": "us-west-2" }, { "CreationTime": 1469498497.792, "CurrentPlayerSessionCount": 3, "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "insane"}, {"Key": "gameMap","Value": "Dystopia"}, {"Key": "gameMode","Value": "FFA"} ], "GameSessionId": "gsess-7777dddd-55ee-66ff-44aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 10, "Name": "Matt's Awesome Game win456", "Port": "8080", "Status": "ACTIVE", "Location": "ca-central-1" } ] }

Search and sort game sessions

In this example, we want to find all game sessions that allow 20 or more players and are currently accepting new players. We want the results to be sorted so that the newest game sessions are returned first.

This example illustrates a search of a single fleet location. The requested fleet, which resides in us-west-2, also has game sessions in remote locations, including ap-southeast-2. As shown, the results are limited to the requested fleet location.

Sample Request

{ "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "Location": "ap-southeast-2", "FilterExpression": "maximumSessions>=20 AND hasAvailablePlayerSessions=true", "SortExpression": "creationTimeMillis DESC" "Limit": 2 } CLI syntax: aws gamelift search-game-sessions --fleet-id "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa" --location "ap-southeast-2" --filter-expression "maximumSessions=20 AND hasAvailablePlayerSessions=true" --sort-expression "creationTimeMillis DESC"

Sample Response

{ "GameSessions": [ { "CreationTime": 1469498497.792, "CurrentPlayerSessionCount": 3, "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "hard"}, {"Key": "gameMap","Value": "Dystopia"}, {"Key": "gameMode","Value": "Brawl"} ], "GameSessionId": "gsess-7777dddd-55ee-66ff-44aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 20, "Name": "Matt's Awesome Game win456", "Port": "8080", "Status": "ACTIVE", "Location": "ap-southeast-2" }, { "CreationTime": 1469498468.057, "CurrentPlayerSessionCount": , "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "easy"}, {"Key": "gameMap","Value": "Snowfall"}, {"Key": "gameMode","Value": "Explore"} ], "GameSessionId": "gsess-4444dddd-55ee-66ff-77aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 50, "Name": "Matt's Awesome Game win123", "Port": "8080", "Status": "ACTIVE", "Location": "ap-southeast-2" } ] }

Search game sessions by custom game properties

This example searches for game sessions based on game map and game mode information, which is stored as key-value pairs in the GameProperties of a GameSession. In this example, we want to find all game sessions where gameMode is Ffa (free-for-all), and gameMap is either "Suzuka" or "Silverstone". We are sorting our results by gameSessionProperties.difficulty (with possible values of "novice", "easy", "normal", "hard", or "insane"). Note: "Value is evaluated as a string, so the sorted results will be listed by the alphabetic order of the difficulty values.

Sample Request

{ "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "Location": "us-west-2", "FilterExpression": "gameSessionProperties.gameMode = 'Ffa' AND gameSessionProperties.gameMap = 'Suzuka' OR gameSessionProperties.gameMap = 'Silverstone'", "SortExpression": "gameSessionProperties.difficulty ASC" "Limit": 2 } CLI syntax: aws gamelift search-game-sessions --fleet-id "9999ffff-88ee-77dd-66cc-5555bbbb44aa" --filter-expression "gameSessionProperties.gameMode = 'Ffa' AND gameSessionProperties.gameMap = 'Suzuka' OR gameSessionProperties.gameMap = 'Silverstone'" --sort-expression "gameSessionProperties.difficulty DESC"

Sample Response

{ "GameSessions": [ { "CreationTime": 1469498468.057, "CurrentPlayerSessionCount": 5, "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "easy"}, {"Key": "gameMap","Value": "Suzuka"}, {"Key": "gameMode","Value": "Ffa"} ], "GameSessionId": "gsess-4444dddd-55ee-66ff-77aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 10, "Name": "Matt's Awesome Game win123", "Port": "8080", "Status": "ACTIVE" "Location": "us-west-2" }, { "CreationTime": 1469498497.792, "CurrentPlayerSessionCount": 3, "FleetId": "fleet-9999ffff-88ee-77dd-66cc-5555bbbb44aa", "GameProperties": [ {"Key": "difficulty","Value": "normal"}, {"Key": "gameMap","Value": "Silverstone"}, {"Key": "gameMode","Value": "Ffa"} ], "GameSessionId": "gsess-7777dddd-55ee-66ff-44aa-8888bbbb99cc", "IpAddress": "192.0.2.0", "MaximumPlayerSessionCount": 10, "Name": "Matt's Awesome Game win456", "Port": "8080", "Status": "ACTIVE" "Location": "us-west-2" } ] }

See Also

For more information about using this API in one of the language-specific Amazon SDKs, see the following: