The keepAlive feature lets you keep sessions alive across disconnects, so you can reconnect and continue using the same session.
| When connection closes | |
|---|---|
| Without keep alive | Session ends |
| With keep alive | Session stays available for reconnection |
Why use keep alive?
- Reconnection workflows: Connect, disconnect, and reconnect to the same session
- Multiple connections: Different scripts or tools can connect to the same session
- Survive disconnects: Network issues won’t end your session
- Performance: Reconnecting to an existing session is faster than creating a new one
- Session reuse: Re-run scripts against the same session without recreating it
- Billing optimization: keep alive lets you avoid minimum browser usage charges if running many short-lived sessions (under 1 minute)
Using keep alive
Creating a keep alive session
Setting keepAlive to true keeps the session available for later use. Reconnect to the keep alive session using the same connect URL.
import Browsers from "agentmq";
const browsers = new Browsers({ apiKey: process.env.AGENTMQ_TOKEN! });
const session = await browsers.sessions.create({
keepAlive: true,
});
// Reconnect later using the same connect URL
console.log("Connect URL:", session.connectUrl);from agentmq.browsers import Browsers
import os
browsers = Browsers(api_key=os.environ["AGENTMQ_TOKEN"])
session = browsers.sessions.create(
keep_alive=True
)
# Reconnect later using the same connect URL
print("Connect URL:", session.connect_url)Releasing a keep alive session
Keep alive sessions must be explicitly released to stop; otherwise, they’ll continue until the session’s timeout.
await browsers.sessions.update(session.id, {
status: "REQUEST_RELEASE",
});const headers = {
"Authorization: Bearer": "<your-api-key>",
"Content-Type": "application/json",
};
const body = JSON.stringify({
status: "REQUEST_RELEASE",
});
fetch("https://api.agent.mq/v1/browsers/sessions/<session-id>", {
method: "POST",
headers,
body,
})
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));browsers.sessions.update(
session.id,
status="REQUEST_RELEASE"
)import os
import requests
headers = {"Authorization: Bearer": os.environ["AGENTMQ_TOKEN"]}
json = {
"status": "REQUEST_RELEASE",
}
response = requests.post(
f"https://api.agent.mq/v1/browsers/sessions/<session-id>",
headers=headers,
json=json,
)
# Raise an exception if there wasn't a good response from the endpoint
response.raise_for_status()
print(response.json())Keep alive and timeouts
Keep alive sessions still respect session timeouts. If you need a session that both survives disconnects and runs for an extended period, configure both options:
const session = await browsers.sessions.create({
keepAlive: true,
timeout: 3600, // 1 hour
});