# server.Server

- **Module:** server
- **Package:** libtmux
- **Language:** TypeScript
- **Kind:** class
- **Source:** https://github.com/libtmux/libtmux-ts/blob/46cccfef2a546de55ce8b308249b8c76339089a0/packages/libtmux/src/server.ts#L281
- **Page:** https://libtmux.org/en/ts/latest/reference/server-server/

One tmux server, addressed by the socket it listens on.

Constructing this starts no process. tmux spawns the daemon on the first
command that needs one, and a daemon already listening on that socket is
used rather than replaced, so two [`Server`](/en/ts/latest/reference/server-server/) values built from the same
options address the same tmux.

Sessions, windows and panes are reached from here and carry the server with
them. Handles taken from one daemon do not survive its restart: the
replacement numbers panes from `%0` again — see [`DaemonIdentity`](/en/ts/latest/reference/server-daemonidentity/).

Save this complete program as `server.ts`. With Bun 1.4.2 or newer,
tmux 3.2a or newer, and `libtmux` installed, run `bun run server.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

```ts
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 } 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 {
  assert.equal(await server.hasSession("work"), false);
  const session = await server.newSession({
    name: "work",
    windowName: "editor",
    shellCommand: "cat",
  });
  assert.equal(await server.hasSession("work"), true);
  const snapshot = await server.snapshot();
  assert.equal(snapshot.sessions.one({ name: "work" }).id, session.id);
  assert.equal(snapshot.windows.count(), 1);
  assert.equal(snapshot.panes.count(), 1);
  console.log("work: 1 session, 1 window, 1 pane");
} 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");
```

## Members

### Collections and queries

- [`sessions`](/en/ts/latest/reference/server-server-sessions/) (method): Every session on the server, read now.
- [`windows`](/en/ts/latest/reference/server-server-windows/) (method): Every window on the server, including each placement of a linked window.
- [`panes`](/en/ts/latest/reference/server-server-panes/) (method): Every pane on the server.
- [`clients`](/en/ts/latest/reference/server-server-clients/) (method): Every client attached to the server.
- [`listBuffers`](/en/ts/latest/reference/server-server-listbuffers/) (method): Every buffer name this server holds.
- [`listCommands`](/en/ts/latest/reference/server-server-listcommands/) (method): Every command name the running tmux understands.
- [`snapshot`](/en/ts/latest/reference/server-server-snapshot/) (method): Acquire an immutable view of the server at this instant.

### Common operations

- [`newSession`](/en/ts/latest/reference/server-server-newsession/) (method): Create a detached session and resolve it as a handle.
- [`kill`](/en/ts/latest/reference/server-server-kill/) (method): Terminate the tmux server and every session on it.
- [`sourceFile`](/en/ts/latest/reference/server-server-sourcefile/) (method): Run a tmux config file against this server.
- [`showOptions`](/en/ts/latest/reference/server-server-showoptions/) (method): Every server-scope option tmux currently reports.
- [`showHooks`](/en/ts/latest/reference/server-server-showhooks/) (method): Every global hook tmux currently reports.
- [`setBuffer`](/en/ts/latest/reference/server-server-setbuffer/) (method): Put a string into a named paste buffer.
- [`isAlive`](/en/ts/latest/reference/server-server-isalive/) (method): Whether the tmux server is reachable.
- [`showEnvironment`](/en/ts/latest/reference/server-server-showenvironment/) (method): Every variable in the server's global environment.
- [`showBuffer`](/en/ts/latest/reference/server-server-showbuffer/) (method): Read a named paste buffer's contents.
- [`deleteBuffer`](/en/ts/latest/reference/server-server-deletebuffer/) (method): Discard a named paste buffer.
- [`runShell`](/en/ts/latest/reference/server-server-runshell/) (method): Run a shell command through tmux and return whatever it printed.
- [`cmd`](/en/ts/latest/reference/server-server-cmd/) (method): Run a tmux command this package does not model.
- [`setOption`](/en/ts/latest/reference/server-server-setoption/) (method): Set a server-scope option.
- [`setEnvironment`](/en/ts/latest/reference/server-server-setenvironment/) (method): Set a variable in the server's global environment.
- [`unsetEnvironment`](/en/ts/latest/reference/server-server-unsetenvironment/) (method): Drop a variable from the server's global environment entirely.
- [`hasSession`](/en/ts/latest/reference/server-server-hassession/) (method): Whether a session with this name exists.
- [`unsetOption`](/en/ts/latest/reference/server-server-unsetoption/) (method): Remove a server-scope option.
- [`loadBuffer`](/en/ts/latest/reference/server-server-loadbuffer/) (method): Fill a paste buffer from data fed through tmux's stdin.
- [`saveBuffer`](/en/ts/latest/reference/server-server-savebuffer/) (method): Write a paste buffer to a file instead of reading it back.
- [`setHook`](/en/ts/latest/reference/server-server-sethook/) (method): Bind a tmux command to a global hook.
- [`unsetHook`](/en/ts/latest/reference/server-server-unsethook/) (method): Remove a global hook.
- [`removeEnvironment`](/en/ts/latest/reference/server-server-removeenvironment/) (method): Mark a variable in the server's global environment for removal from the environment of processes tmux starts, leaving the entry in place.
- [`ifShell`](/en/ts/latest/reference/server-server-ifshell/) (method): Run one command or another depending on a condition.
- [`raiseIfDead`](/en/ts/latest/reference/server-server-raiseifdead/) (method): Assert the server is reachable, raising with tmux's reason if not.
- [`version`](/en/ts/latest/reference/server-server-version/) (method): The tmux version this server is running.
- [`configFile`](/en/ts/latest/reference/server-server-configfile/) (method): The configuration file this server was pointed at, if any.
- [`socketPath`](/en/ts/latest/reference/server-server-socketpath/) (method): The socket path this server addresses, if it was given one.
- [`showGlobalOptions`](/en/ts/latest/reference/server-server-showglobaloptions/) (method): Read the defaults every session or window inherits.
- [`setGlobalOption`](/en/ts/latest/reference/server-server-setglobaloption/) (method): Set a default every session or window inherits.
- [`getEnvironment`](/en/ts/latest/reference/server-server-getenvironment/) (method): One variable from the server's global environment, or `undefined` when tmux carries no entry.
- [`colors`](/en/ts/latest/reference/server-server-colors/) (method): The terminal color override; undefined leaves detection to tmux.
- [`socketName`](/en/ts/latest/reference/server-server-socketname/) (method): The socket name this server addresses, if it was named rather than pathed.
- [`batch`](/en/ts/latest/reference/server-server-batch/) (method): Run planned mutations in order, resolving each to what it made.
- [`connect`](/en/ts/latest/reference/server-server-connect/) (method): Bind this server to one control-mode connection and return it.
- [`pipeline`](/en/ts/latest/reference/server-server-pipeline/) (method): Run several tmux commands in order.
- [`watch`](/en/ts/latest/reference/server-server-watch/) (method): Stream tmux's control-mode notifications until the stream is disposed.
- [`checkAlive`](/en/ts/latest/reference/server-server-checkalive/) (method): Assert the server is reachable, raising with tmux's reason if not.
- [`daemonIdentity`](/en/ts/latest/reference/server-server-daemonidentity/) (method): Which daemon is answering on this socket right now.
- [`equals`](/en/ts/latest/reference/server-server-equals/) (method): Whether `other` is a handle addressing the same tmux server.
- [`showBufferBytes`](/en/ts/latest/reference/server-server-showbufferbytes/) (method): Read a named paste buffer without decoding or splitting its bytes.
- [`showResolvedOptions`](/en/ts/latest/reference/server-server-showresolvedoptions/) (method): The option values that govern this server, own and inherited together.
- [`tmuxBin`](/en/ts/latest/reference/server-server-tmuxbin/) (method): The tmux executable this server runs.
- [`unsetGlobalOption`](/en/ts/latest/reference/server-server-unsetglobaloption/) (method): Remove a default, so tmux falls back to its own built-in value.
- [`validateLayouts`](/en/ts/latest/reference/server-server-validatelayouts/) (method): Check every planned window layout before setup scripts or mutations.
- [`versionAtLeast`](/en/ts/latest/reference/server-server-versionatleast/) (method): Whether this server is at least `minimum`, written the way tmux writes it.
- [`withConnection`](/en/ts/latest/reference/server-server-withconnection/) (method): Run `body` against a connected server, closing it afterwards.

### Other members

- [`constructor`](/en/ts/latest/reference/server-server-constructor/) (method)
- [`serverBrand`](/en/ts/latest/reference/server-server-serverbrand/) (attribute)
- [`toString`](/en/ts/latest/reference/server-server-tostring/) (method): How this server renders in a log line or an error.

## Guides

Pages that discuss `Server`.

### Concepts

- [Filtering and queries](/en/ts/latest/concepts/queries/)
- [Server, session, window, pane](/en/ts/latest/concepts/server-session-window-pane/)

### Guides

- [Attaching to tmux](/en/ts/latest/guides/attaching-to-tmux/)
- [Testing with libtmux](/en/ts/latest/guides/testing-with-libtmux/)

### Ports

- [TypeScript MCP examples](/en/ts/latest/mcp/examples/)
- [TypeScript MCP API](/en/ts/latest/mcp/reference/)
- [TypeScript workspace internals](/en/ts/latest/workspace/internals/)
- [Use the TypeScript workspace builder](/en/ts/latest/workspace/internals/guides/)
- [TypeScript workspace builder API](/en/ts/latest/workspace/reference/)

## In other ports

A tmux server

| Language | Equivalent |
| --- | --- |
| Python | [`libtmux.Server`](/en/py/latest/reference/libtmux-server/) (class) |
| Ruby | [`LibTmux::Server`](/en/ruby/latest/reference/libtmux-server/) (class) |
| Lua | [`libtmux.Server`](/en/lua/latest/reference/libtmux-server/) (class) |
| PowerShell | [`LibTmux\New-TmuxServer`](/en/powershell/latest/reference/libtmux-new-tmuxserver/) (cmdlet). This cmdlet constructs a shared .NET `LibTmux.Server` handle without contacting tmux. |
| Julia | [`LibTmux.Server`](/en/julia/latest/reference/libtmux-server/) (struct). `Server` describes an endpoint without contacting tmux. Read its hierarchy with `snapshot`. |
| Elixir | [`LibTmux.Server`](/en/elixir/latest/reference/libtmux-server/) (struct). `Server` describes an existing server by executable and socket. It neither connects nor reserves the server’s identity. |
| Rust | [`server.Server`](/en/rs/latest/reference/server-server/) (struct) |
| Go | [`tmux.Server`](/en/go/latest/reference/tmux-server/) (struct) |
| Java | [`io.github.libtmux.Server`](/en/java/latest/reference/io-github-libtmux-server-server/) (class) |
| Kotlin | [`io.github.libtmux.kotlin.Server`](/en/kotlin/latest/reference/io-github-libtmux-kotlin-server/) (class) |
| Scala | [`io.github.libtmux.scaladsl.Server`](/en/scala/latest/reference/io-github-libtmux-scaladsl-server/) (typealias) |
| C# | [`LibTmux.Server`](/en/csharp/latest/reference/libtmux-server/) (class) |
| F# | [`LibTmux.FSharp.Server`](/en/fsharp/latest/reference/libtmux-fsharp-server/) (module). This F# module provides functions over the shared .NET `LibTmux.Server` type. |
| C++ | [`libtmux::Server`](/en/cxx/latest/reference/libtmux-server/) (class) |
| Swift | [`Server`](/en/swift/latest/reference/server/) (struct) |
