[vim/vim] revenge: Add ch_listen() function for socket server support (PR #19231)

11 views
Skip to first unread message

mattn

unread,
Jan 21, 2026, 5:09:31 AM (4 days ago) Jan 21
to vim/vim, Subscribed

Summary

#3639

This patch adds ch_listen() function to Vim, allowing scripts to create listening sockets and accept incoming network connections. This enables Vim to act as a simple network server for various use cases.

Changes

New Functions

  • ch_listen(address [, options]) - Listen on a socket and accept connections

    • Returns a channel representing the listening socket
    • Calls a callback function when a new client connects
    • Supports both IPv4 and Unix domain sockets
  • channel_listen_unix(path, callback) - Unix domain socket support

  • channel_listen_func(argvars) - Vim script interface

Implementation Details

  • Added ch_listen field to struct channel_S to mark listening sockets
  • Modified channel_read() to handle accept() on listening sockets
  • When a connection is accepted, a new channel is created and passed to the callback
  • The callback receives the accepted channel and client address

API Usage

function! OnConnect(channel, address)
    echomsg 'Client connected from: ' . a:address
    call ch_setoptions(a:channel, {
        \ 'callback': function('OnData'),
    \ })
endfunction

function! OnData(channel, data)
    echomsg 'Received: ' . a:data
endfunction

let server = ch_listen('127.0.0.1:8080', {
    \ 'callback': function('OnConnect'),
\ })

Use Cases

  • Simple HTTP server for serving static files
  • Network protocol implementation for testing
  • Local IPC between Vim and other processes
  • Learning network programming concepts

Testing

  • Implemented test case Test_listen() in test_channel.vim
  • Tested with multiple concurrent connections
  • Verified callback invocation and data transmission

Related Features

This complements the existing ch_open() function:

  • ch_open() - connect to a server
  • ch_listen() - accept connections as a server

Limitations

  • No SSL/TLS support
  • Single-threaded (blocks Vim event loop during processing)
  • No built-in HTTP parsing (simple callbacks only)

This is demonstration of HTTP server written in Vim script.

image.png (view on web)

You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/19231

Commit Summary

File Changes

(7 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231@github.com>

mattn

unread,
Jan 21, 2026, 5:21:40 AM (4 days ago) Jan 21
to vim/vim, Subscribed
mattn left a comment (vim/vim#19231)

I'll fix test fail in later.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/c3777297345@github.com>

mattn

unread,
Jan 21, 2026, 5:46:04 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/61d4cdacc9907894bc7a17578bb7ced62a957a2d/after/2d56f553297f04b74ad87fbdc80d3be7aef256a5@github.com>

mattn

unread,
Jan 21, 2026, 5:53:09 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/2d56f553297f04b74ad87fbdc80d3be7aef256a5/after/bf360c48f8912c2cb76d114de6ede6eeb61f7131@github.com>

mattn

unread,
Jan 21, 2026, 6:05:08 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/bf360c48f8912c2cb76d114de6ede6eeb61f7131/after/28d6f7ab946fda985d283b600869a6610f2288d2@github.com>

mattn

unread,
Jan 21, 2026, 6:09:51 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/28d6f7ab946fda985d283b600869a6610f2288d2/after/2fe4734196247b8cfdc9f0577220501dd2a78106@github.com>

mattn

unread,
Jan 21, 2026, 6:20:55 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/2fe4734196247b8cfdc9f0577220501dd2a78106/after/72aaa64f881bcc277f8c701c173b49956737491d@github.com>

mattn

unread,
Jan 21, 2026, 6:32:43 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/72aaa64f881bcc277f8c701c173b49956737491d/after/d0ff4c9f48a17ff57422d54458b96dd3bf6a5f73@github.com>

mattn

unread,
Jan 21, 2026, 6:34:10 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/d0ff4c9f48a17ff57422d54458b96dd3bf6a5f73/after/b37fc0f202b595131b4aaf967ccd40e14628a8c5@github.com>

mattn

unread,
Jan 21, 2026, 7:02:08 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

  • 58661c8 fix order in function list

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/b37fc0f202b595131b4aaf967ccd40e14628a8c5/after/58661c822dbf8c536e63352953f997b43d00fd89@github.com>

mattn

unread,
Jan 21, 2026, 7:10:25 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/58661c822dbf8c536e63352953f997b43d00fd89/after/c61af317834d683c5f5fc88698ee058252285649@github.com>

mattn

unread,
Jan 21, 2026, 7:51:53 AM (4 days ago) Jan 21
to vim/vim, Push

@mattn pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/before/c61af317834d683c5f5fc88698ee058252285649/after/95c71009883c2e9030e66e8f4617fb499efcf793@github.com>

Christian Brabandt

unread,
Jan 21, 2026, 2:32:48 PM (4 days ago) Jan 21
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#19231)

Thanks that is nice. But I'll move this after the Vim 9.2 release.
Can a few people try it out? I'd also think that we should have a very simple practical example for this in the help.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/c3780776946@github.com>

bfrg

unread,
Jan 24, 2026, 12:41:35 PM (23 hours ago) Jan 24
to vim/vim, Subscribed
bfrg left a comment (vim/vim#19231)

It is not clear from the docs how ch_listen() differs from ch_open({address} [, {options}])? See :h channel-address for possible values of address.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/c3795227881@github.com>

mattn

unread,
Jan 24, 2026, 2:01:37 PM (22 hours ago) Jan 24
to vim/vim, Subscribed
mattn left a comment (vim/vim#19231)

@chrisbra This is simple practice example working as HTTP file server.

https://gist.github.com/mattn/3e691419c37fe7ce501dc37e2a1bb545


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19231/c3795360900@github.com>

Reply all
Reply to author
Forward
0 new messages