# libtmux.OwnedServer.server

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Lua
- **Kind:** attribute
- **Source:** https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/lua/libtmux/_internal/server.lua#L1226
- **Page:** https://libtmux.org/en/lua/latest/reference/libtmux-ownedserver-server/

A handle to one running tmux server, reached through a connected runtime.

 `libtmux.Runtime:connect` resolves to a server for a daemon that is already
 running; it never starts one, and closing the handle leaves the daemon
 running. `libtmux.Runtime:start_server` starts a private daemon and owns it
 (see `libtmux.OwnedServer`). With no socket named, `connect` passes tmux
 none, and tmux picks the server (see `libtmux.ConnectOptions`). A method that talks to tmux
 returns a `libtmux.Request`: call `libtmux.Request:await` from a task the
 runtime manages, or pass a callback to `libtmux.Request:on_complete`. A
 failed request reports a `libtmux.Error`. If the daemon restarts or its
 socket is replaced, the handle stops working and the caller connects again.

 # What is on here

 A tmux server does a great many things, so this class has a great many
 methods. They fall into a few groups:

 **Connecting.** `libtmux.Runtime:connect` takes `libtmux.ConnectOptions`: an
 absolute path to the tmux binary and, optionally, to the daemon's socket.
 `Server:close` closes the connections the handle owns. `Server:observe`
 takes a session handle and resolves to a `libtmux.Observation`, a
 control-mode view of that session whose watches report pane output,
 notifications and format values.

 `libtmux.Runtime:connect`, `Server:close` and `Server:observe`

 **Finding one thing.** Take one record from a `libtmux.Snapshot`, or from
 the rows of a query, with `libtmux.Selection:one`, which reports no match or
 several as errors, or `libtmux.Selection:one_or_nil`, which also accepts no
 match. Pass it, with its snapshot, to `Server:handle`: that binds the record
 to a live session, window, window link, pane, client or buffer handle of the
 same class, and returns it directly rather than as a request.

 `libtmux.Selection:one`, `libtmux.Selection:one_or_nil` and `Server:handle`

 **Listing everything.** `Server:snapshot` lists sessions, windows, window
 links, panes, clients and buffers, and resolves to a `libtmux.Snapshot`
 whose collections are selections. The capture spans several tmux commands,
 so it is not an atomic view: observed races land in `libtmux.Snapshot.races`
 and clear `libtmux.Snapshot.complete`, and `libtmux.SnapshotOptions.strict`
 adds a verification pass.

 `Server:snapshot`, `libtmux.Snapshot.races`, `libtmux.Snapshot.complete` and
 `libtmux.SnapshotOptions.strict`

 **Querying things and getting around.** `Server:query` reads live state and
 resolves to the rows that match a criteria table, with the snapshot and plan
 behind them; `Server:query_panes` does the same for panes. `Server:explain`
 and `Server:explain_panes` return the plan without contacting tmux. On a
 snapshot already in hand, `libtmux.Selection:where` and
 `libtmux.Selection:filter` narrow a collection in memory, and
 `libtmux.Selection:first`, `libtmux.Selection:count` and
 `libtmux.Selection:exists` inspect it; the `libtmux.query` module does the
 same for ordinary Lua tables. Records lead to each other without another
 command: `libtmux.SnapshotSession.window_links`,
 `libtmux.SnapshotWindow.panes`, `libtmux.SnapshotPane.window`,
 `libtmux.SnapshotWindowLink.session` and
 `libtmux.SnapshotWindowLink.window`.

 `Server:query`, `Server:query_panes`, `Server:explain`,
 `Server:explain_panes`, `libtmux.Selection:where`,
 `libtmux.Selection:filter`, `libtmux.Selection:first`,
 `libtmux.Selection:count`, `libtmux.Selection:exists`, `libtmux.query`,
 `libtmux.SnapshotSession.window_links`, `libtmux.SnapshotWindow.panes`,
 `libtmux.SnapshotPane.window`, `libtmux.SnapshotWindowLink.session` and
 `libtmux.SnapshotWindowLink.window`

 **Changing things.** `Server:new_session` creates a detached session and
 resolves to a `libtmux.Creation` holding handles for the session and the
 window, pane and window link it starts with. `Server:switch_client` points
 an attached client, selected by the name and TTY a snapshot reports, at a
 session handle, and `Server:detach_client` detaches it.

 `Server:new_session`, `Server:switch_client` and `Server:detach_client`

 **Options and hooks.** `Server:get_option`, `Server:list_options`,
 `Server:set_option` and `Server:unset_option` address the server's own
 options; pass `scope = "global_session"` or `"global_window"` for tmux's
 global defaults. `Server:get_hook`, `Server:list_hooks`, `Server:set_hook`
 and `Server:unset_hook` need one of those two scopes spelled out. The global
 environment has `Server:get_environment`, `Server:list_environment` and
 `Server:set_environment`; `Server:unset_environment` deletes a name, while
 `Server:remove_environment` records a removal marker instead.

 `Server:get_option`, `Server:list_options`, `Server:set_option`,
 `Server:unset_option`, `Server:get_hook`, `Server:list_hooks`,
 `Server:set_hook`, `Server:unset_hook`, `Server:get_environment`,
 `Server:list_environment`, `Server:set_environment`,
 `Server:unset_environment` and `Server:remove_environment`

 **Everything else tmux keeps.** Paste buffers, always by explicit name:
 `Server:set_buffer` stores bytes, `Server:show_buffer` reads them back and
 `Server:delete_buffer` removes the buffer. None infers the most recent
 buffer. Key bindings, configuration files, shell jobs and wait-for channels
 have no method here; reach them through `Server:command`.

 `Server:set_buffer`, `Server:show_buffer`, `Server:delete_buffer` and
 `Server:command`

 **Things that need a terminal.** None. The luv and Neovim adapters do not
 own an interactive terminal, so `libtmux.Session:attach` returns
 `unsupported_tty`.

 `libtmux.Session:attach`

 **Running a command.** Anything tmux can do that is not here is reachable
 through `Server:command`, which runs one tmux command from literal arguments
 and resolves to its stdout, stderr, exit code and signal; a nonzero exit is
 reported in that result, not as a failure. `Server:group` sends several
 commands as one ordered group with a single aggregate result, and
 `Server:batch` runs independent commands and reports an outcome for each.

 `Server:command`, `Server:group` and `Server:batch`

## Examples

Start a private tmux server, create a session on it, and stop it. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:

```console
$ luarocks --tree ./rocks install luv &&
  luarocks --tree ./rocks install libtmux &&
  eval "$(luarocks --tree ./rocks path)"
```

Save the complete program as `start_server.lua`. It starts its own private tmux server, uses it and stops it, so no existing session is needed.

[Source example](https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/examples/api/start_server.lua).

```lua
local adapter = require("libtmux.runtime.luv")

local function must(value, err)
    if err ~= nil then
        error(tostring(err), 0)
    end
    return value
end

-- tmux is /usr/bin/tmux unless TMUX_BIN names another absolute path.
local binary = os.getenv("TMUX_BIN") or "/usr/bin/tmux"

must(adapter.run(function(runtime)
    -- The server is private: its own directory, socket and environment.
    local owned = must(runtime:start_server({ binary = binary }):await())
    local server = owned.server
    local before = must(server:snapshot():await())

    must(server:new_session({ name = "demo", argv = { "/bin/cat" } }):await())
    local after = must(server:snapshot():await())
    print(("sessions: %d, then %d"):format(#before.sessions, #after.sessions))

    must(owned:stop():await())
    return true
end))
```

Run the saved program. It runs `/usr/bin/tmux`; set `TMUX_BIN` to another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:

```console
$ eval "$(luarocks --tree ./rocks path)" &&
  lua start_server.lua
```

## Guides

Pages that discuss `server`.

- [Switch and detach clients](/en/lua/latest/guides/clients/)
- [Getting started](/en/lua/latest/guides/overview/)
- [Runtime ownership](/en/lua/latest/guides/runtime/)
