Channel Types
Nvim supports several types of channels for different use cases:RPC Channels
MessagePack-RPC protocol for API calls and events
Job Channels
Communicate with spawned processes via stdio
Socket Channels
TCP/IP or Unix domain socket connections
Stdio Channels
Standard input/output in headless mode
Opening Channels
There are several ways to open a channel in Nvim:1. Stdio Channel
Usestdioopen() when running Nvim in headless mode:
2. Job Channel
Usejobstart() to spawn a process and communicate via stdin/stdout:
3. Socket Channel
Usesockconnect() to connect to a TCP/IP or Unix socket:
4. Listening Socket
Start a server that listens for connections:Channel Modes
Channels operate in different modes:Raw Bytes Mode
Default mode for job channels. Data is sent and received as raw bytes:RPC Mode
Using MessagePack-RPC protocol for API calls:PTY Mode
Pseudo-terminal for interactive programs:Channel Callbacks
Channel Information
List All Channels
Get information about all open channels:Get Channel Info
Query detailed information about a specific channel:id- Channel identifierstream- Stream type:stdio,socket,job,stderrmode- Communication mode:bytes,terminal,rpcpty- PTY name (if applicable)buffer- Connected buffer (for terminal)client- Client information (set bynvim_set_client_info())
Example: Find UI Channels
Sending Data
chansend()
Send raw data to a channel:nvim_chan_send()
API function for sending data:RPC Functions
For RPC channels, userpcrequest() and rpcnotify():
Channel Lifecycle
Advanced Patterns
Bidirectional RPC
Embed Nvim in another Nvim instance:Channel with Timeout
Communicating Between Nvim Instances
Error Handling
Handle channel errors gracefully:Best Practices
Performance Tips:
- Use buffered mode for operations that need complete output
- Handle partial lines correctly in streaming mode
- Close channels when done to free resources
- Use RPC mode for structured communication
Security Considerations:
- Localhost TCP sockets are less secure than Unix domain sockets
- RPC channels are implicitly trusted - they can call any API function
- Validate input from external processes
- Use
v:servernameto avoid hardcoding socket paths