Server.sessions
Every session on the server, read now.
Source and package
-
Description#
This and its three siblings each take a snapshot of their own — one daemon identity read and four listings per call — so calling several in a row describes several different instants and pays for each. Inside a loop that is an N+1: prefer one
snapshot()and readsessions(),windows(),panes(), andclients()off it, which is both cheaper and consistent.Save this complete program as
sessions.ts. With Bun 1.4.2 or newer, tmux 3.2a or newer, andlibtmuxinstalled, runbun run sessions.ts. It creates and stops a private tmux server. A failed operation or cleanup exits with an error; a cleanup error names the retained socket directory.Examples#
import { strict as assert } from "node:assert";import { mkdtemp, readdir, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { Server, NoMatchError } from "libtmux";const directory = await mkdtemp(join(tmpdir(), "ltx-example-"));const server = new Server({socketPath: join(directory, "tmux.sock"),configFile: "/dev/null",timeoutMs: 5_000,});const failures: unknown[] = [];try {await server.newSession({ name: "prod-api", shellCommand: "cat" });await server.newSession({ name: "dev-api", shellCommand: "cat" });const sessions = await server.sessions();assert.equal(sessions.count(), 2);console.log("sessions:",sessions.map((item) => item.name).sort().join(", "),);const production = sessions.where({ name: { startsWith: "prod-" } }).one();assert.equal(production.name, "prod-api");console.log("production:", production.name);assert.equal(sessions.oneOrUndefined({ name: "missing" }), undefined);try {sessions.one({ name: "missing" });throw new Error("Expected the missing session to fail lookup");} catch (error) {if (!(error instanceof NoMatchError)) throw error;console.log("missing:", error.code);}} catch (error) {failures.push(error);} finally {try {if ((await readdir(directory)).includes("tmux.sock")) await server.kill();await rm(directory, { recursive: true });} catch (error) {failures.push(new Error(`Cleanup failed; inspect ${directory}`, { cause: error }));}}if (failures.length > 0) throw new AggregateError(failures, "Example failed");
Guides#
Pages that discuss sessions.
- Concepts
- Topics
In other ports#
List the server’s sessions
| Language | Equivalent |
|---|---|
| Python | libtmux |
| Ruby | Lib |
| Lua | libtmux |
| PowerShell | Lib Acquires sessions through -Server, with optional exact -Id and -Name selectors. |
| Julia | Lib sessions reads membership from a captured Snapshot without contacting tmux. |
| Elixir | Lib Returns a Selection of captured sessions without contacting tmux. |
| Rust | server |
| Go | tmux |
| Java | io |
| Kotlin | io |
| Scala | io |
| C# | Lib |
| F# | Lib Builds a deferred session query on LibTmux.Server. Query.list acquires the results; it does not traverse the handle’s captured membership. |
| C++ | libtmux:: |
| Swift | Server |