Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardssh/1b70c48319d26dbdac92c3c6f187da0a to your computer and use it in GitHub Desktop.
Save leonardssh/1b70c48319d26dbdac92c3c6f187da0a to your computer and use it in GitHub Desktop.
coc-discord-rpc on WSL: passthrough to Discord on host Windows

Using coc-discord-rpc on WSL

You can use npiperelay to let coc-discord-rpc connect to a Discord instance running on the host Windows operating system. It requires modifying the extension a bit, but it's relatively painless. This guide assumes you have coc.nvim and coc-discord-rpc already installed.

Get npiperelay and socat

To build npiperelay, you'll need Go. Grab the golang package from your favorite package manager and follow the instructions on the npiperelay repo. You'll also need to install socat if it doesn't come with your distribution.

Modify the coc-discord-rpc extension

To locate the Discord IPC socket, the coc-discord-rpc extension looks in the subdirectory of /tmp/ created for NeoVim when it starts. Discord, however, puts its IPC in the root of /tmp/, not a subdirectory. And since we will be starting socat before NeoVim, we won't be able to put the forwarded socket in its subdirectory. We have to modify the extension to look in the correct directory. Open the main source file, which should be located at ~/.config/coc/extensions/node_modules/coc-discord-rpc/lib/index.js. Search for the function named getIPCPath.

This function contains a line that sets the path prefix for the IPC socket. It should look something like this:

const prefix = XDG_RUNTIME_DIR || TMPDIR || TMP || TEMP || '/tmp';

Change that line to the following:

const prefix = '/tmp';

Aliasing nvim

Now that the extension is looking in the right place, we have to put an actual IPC socket there. To do so, we need to launch socat before NeoVim to forward the Discord IPC named pipe from Windows. You can do this by creating an alias for nvim that checks if socat is running and launches it in the background if it isn't.

I use fish, so my alias looks something like this:

function nvim
    pidof socat > /dev/null; or socat UNIX-LISTEN:/tmp/discord-ipc-0,fork \
      EXEC:"npiperelay.exe //./pipe/discord-ipc-0"&
    command nvim $argv
end

Or in bash:

nvim () {
    pidof socat > /dev/null 2>&1
    if ! $? -eq 0; then
        socat UNIX-LISTEN:/tmp/discord-ipc-0,fork \
          EXEC:"npiperelay.exe //./pipe/discord-ipc-0"&
    fi
    command nvim "$@"
}

And that's it! Running nvim from your shell of choice should now allow it to connect to the Discord IPC pipe on your host Windows system (as long as you have Discord running, of course).

Note: Using the fork option to socat ensures that you won't get connection errors when running multiple instances of NeoVim. However, only the first opened instance will be able to provide rich presence. If you decide to try to modify coc-discord-rpc to support multiple instances of NeoVim, I'd definitely love to hear about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment