Stay organized with collections Save and categorize content based on your preferences.
You can use WebSockets to create a persistent connection from a client (such as a mobile device or a computer) to an App Engine instance. The open connection allows two-way data exchange between the client and the server at any time, resulting in lower latency and better use of resources.
WebSocketsThe WebSockets protocol, defined in RFC 6455, provides a full-duplex communication channel between a client and a server. The channel is initiated from an HTTP(S) request with an "upgrade" header.
Typical use cases for WebSockets include:
WebSockets are always available to your application without any additional setup. Once a WebSockets connection is established, it will time out after one hour. Usage of the WebSocket is billed by connection usage until timeout or socket termination.
Running a sample application with WebSocketsThe code samples in this document describe how to run a sample application with Websockets.
Prerequisites and setupFollow the instructions in Setting Up Your Development Environment to set up your environment and project, and to understand how apps are structured.
Clone the sample appCopy the sample apps to your local machine, and navigate to the websockets
directory:
Not all clients support WebSockets. To work around this, many applications use libraries such as socket.io that fall back on http long polling with clients that don't support WebSockets.
App Engine typically distributes requests evenly among available instances. However, when using http long polling, multiple sequential requests from a given user need to reach the same instance.
To allow App Engine to send requests by the same user to the same instance, you can enable session affinity. App Engine then identifies which requests are sent by the same users by inspecting a cookie and routes those requests to the same instance.
Important: App Engine applications must always be tolerant of session affinity interruptions, particularly because all App Engine instances are periodically restarted. Enabling session affinity can also limit the effectiveness of App Engine's load balancing algorithms and can cause your instance to become overloaded.Session affinity in App Engine is implemented on a best-effort basis. When developing your app, you should always assume that session affinity is not guaranteed. A client can lose affinity with the target instance in the following scenarios:
Because session affinity isn't guaranteed, you should only use it to take advantage of the ability of socket.io
and other libraries to fall back on HTTP long polling in cases where the connection is broken. You should never use session affinity to build stateful applications.
By default, session affinity is disabled for all App Engine applications. Session affinity is set at the version level of your application and can be enabled or disabled on deployment.
To enable session affinity for your App Engine version, add the following entry to your app.yaml
file:
network:
session_affinity: true
Once the version is deployed with the updated app.yaml, new requests will start serving from the same instance as long as that instance is available.
To turn off session affinity, remove the entry from your app.yaml
file, or set the value to false:
network:
session_affinity: false
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["WebSockets offer a full-duplex communication channel between clients and servers, enabling real-time data exchange and reducing latency."],["Typical use cases for WebSockets include real-time event updates, user notifications, chatting applications, collaborative editing tools, and multiplayer games."],["Session affinity, which routes requests from the same user to the same instance, can be enabled in App Engine to accommodate HTTP long polling as a fallback for clients not supporting WebSockets."],["Session affinity in App Engine is not guaranteed and should be used only for allowing fall back from libraries such as socket.io, and not for stateful applications."],["Session affinity can be enabled or disabled at the version level in App Engine by modifying the `app.yaml` file, ensuring that clients have cookies enabled for it to function."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4