I want to write a vim plugin that listens to Server-sent events. Since I am most fluent with python3 and use neovim, I figured it would be a good idea to use the neovim remote plugin API.
Obviously, listening for messages from the network must not be blocking, so asyncio must be involved somehow. But I was not able to figure out how to combine the two. Somewhere I would have to run an event loop. However, pynvim already runs its own event loop, so I probably should hook into that.
@pynvim.plugin
class MyPlugin:
def __init__(self, nvim):
self.nvim = nvim
@pynvim.command('Connect', nargs='1')
async def connect(self, args):
url = base_url + args[0]
async with sse_client.EventSource(url) as event_source:
for raw in event_source:
try:
msg = json.loads(raw)
except json.JSONDecodeError:
continue
do_something(msg)
This example doesn't work. The Connect command is not available in neovim (it was before I made it async).