Core Library0.xAPI Reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

method libtmux-ts latest

Server.sessions

Every session on the server, read now.

Source and package
Qualified name
Server.sessions
Module
server
Declared in
Server
Package
libtmux
Source
packages/libtmux/src/server.ts
Other languages
PowerShellcmdlet Juliafunction Elixirfunction PythonRubyLuaRustGoJavaC#C++SwiftKotlinScalaF#function
async sessions(): Promise<Selection<Session>>

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 read sessions(), windows(), panes(), and clients() 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, and libtmux installed, run bun 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.

Topics

In other ports#

List the server’s sessions

Equivalents of List the server’s sessions in other languages
Language Equivalent
Python libtmux.Server.sessions
Ruby LibTmux::Server#list_sessions
Lua libtmux.Snapshot.sessions
PowerShell LibTmux\Get-TmuxSession Acquires sessions through -Server, with optional exact -Id and -Name selectors.
Julia LibTmux.sessions sessions reads membership from a captured Snapshot without contacting tmux.
Elixir LibTmux.Snapshot.sessions/1 Returns a Selection of captured sessions without contacting tmux.
Rust server.Server.sessions
Go tmux.Server.Sessions
Java io.github.libtmux.Server.sessions
Kotlin io.github.libtmux.kotlin.Server.sessions
Scala io.github.libtmux.scaladsl.Server.sessions
C# LibTmux.Server.Sessions
F# LibTmux.FSharp.Server.sessions Builds a deferred session query on LibTmux.Server. Query.list acquires the results; it does not traverse the handle’s captured membership.
C++ libtmux::Server::sessions
Swift Server.sessions()
Esc

Type to search.

Reference previewOpen reference