WIP
Websocket implementation for Neovim
{
"samsze0/websocket.nvim",
dependencies = {
"samsze0/utils.nvim"
}
}
local WebsocketClient = require("websocket.client").WebsocketClient
local client = WebsocketClient.new({
connect_addr = "ws://localhost:8080",
on_message = function(message)
print("Received message: " .. message)
end,
on_connect = function()
print("Connected")
end,
on_disconnect = function()
print("Disconnected")
end
})
client:connect()
-- Schedule to run in 2 seconds
vim.defer_fn(function()
client:send("Hello server")
end, 5000)
-- Schedule to run in 5 seconds
vim.defer_fn(function()
client:disconnect()
end, 5000)
Because currently there is no decent Lua/libuv implementations of web servers, so I decided to base this plugin on the Tungstenite crate for the majority of the heavy lifting (i.e. the handling of the websocket protocol). The communication between Neovim/Lua and Rust is facilitated by Rust's FFI (nvim-oxi in combination with mlua). Here is an outline of how it works:
- Neovim/Lua imports the Rust functions (compiled into a dynamic lib) via
require
- Neovim/Lua calls the Rust functions to start the websocket server
- Neovim/Lua stores the callbacks (e.g.
on_message
,on_connected
,on_disconnected
) as global objects, waiting for the Rust code to pull from using mlua
MIT