Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Websocket concurrency issue With multi Modal Live API

Hey Community 
While using the gemini multi modal api with model Gemini 2.0 -Flash - exp. We are facing the below issue while using it via websockets, it sometimes work fine and sometimes it starts giving the below error, Can anyone guide us in implementing a solution for the below error or any one has faced the same issue.
here are the things we have done.
1. We have diagnosed it as a concurrency issue and also made sure that ws.close() is implemented after we close the page.
2. We don't have any websockets open on the client side because we have a single machine as of now connecting to the websockets 
3. Does any body know how much time it takes to close them automatically?

2025-03-05T15:51:18.731934634Z: [INFO]  2025-03-05 15:51:18,713 [ERROR] Error in server message handling: received 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se; then sent 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se
2025-03-05T15:51:18.740701603Z: [INFO]  2025-03-05 15:51:18,714 [ERROR] WebSocket communication error: received 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se; then sent 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se
2025-03-05T15:51:18.741891022Z: [INFO]  2025-03-05 15:51:18,739 [ERROR] WebSocket error: received 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se; then sent 1011 (internal error) Request trace id: 12770ad2c02bc69e, [ORIGINAL ERROR] generic::resource_exhausted: RESOURCE_EXHAUSTED: Maximum concurrent se
2025-03-05T15:52:07.499886225Z: [ERROR]  INFO:     connection closed

0 1 1,361
1 REPLY 1

Hi @legacy-dev,

Welcome to Google Cloud Community!

The "Maximum concurrent sessions" error with the Gemini 2.0 Flash WebSocket API indicates you're exceeding the allowed simultaneous connections.

Here's a breakdown of potential solutions that you can consider to address your issue:

  • Connection Pooling/Re-use: Maintain a pool of pre-established WebSocket connections. Re-use connections from the pool for new requests instead of creating and destroying them repeatedly. This reduces connection overhead and avoids hitting the concurrent connection limit as frequently.
  • Queueing and Rate Limiting: Manage concurrent connections centrally on the server. Track active connections per user/API key. Queue requests if limits are reached. Use asynchronous processing to avoid blocking. Apply a threading.Lock to avoid the race condition.
  • API Rate Limiting Best Practices: Implement exponential backoff for RESOURCE_EXHAUSTED errors. Monitor API usage and adjust request rates accordingly.

You can also refer to the following documents for information:

In addition, take into consideration that the "exp" in the model name indicates that it's experimental. This means that the API might be less stable, have undocumented limitations, or be subject to change. Keep this in mind as you develop your solution.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.