libevent-nim

Nim bindings to the libevent framework

Active Pure Nim score 65/100 · last commit 2026-08-16 · 3 stars · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Stars 3
Forks 0
Open Issues 0
Last Commit 2026-08-16
Downloads 0
Last Indexed 2026-09-06 06:05

Installation

nimble install libevent-nim
choosenim install libevent-nim
git clone https://github.com/openpeeps/libevent-nim

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
libevent-nim - - - - - - -

README

👑 Nim Bindings for LibEvent

nimble install libevent

API reference
Github Actions Github Actions

About

Libevent is an event notification library with a focus on asynchronous IO. It provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. It also supports callbacks due to signals or regular timeouts.

Libevent additionally provides a sophisticated framework for buffered network IO, with support for sockets, filters, rate-limiting, SSL, zero-copy file transmission, and IOCP. Libevent includes support for several useful protocols, including DNS, HTTP, and a minimal RPC framework.

Requirements

  • Libevent 2.1.12 or later
  • Nim 2.0 or later

Create a simple HTTP server

Using the bindings in this package, you can create a simple HTTP server like this:

import std/httpcore
import pkg/libevent

from std/net import Port

let eventBase = event_base_new()
assert eventBase != nil, "Could not create event base"

let httpServer = evhttp_new(eventBase)
assert httpServer != nil, "Could not create HTTP server"

template respond(str: string, code: HttpCode = HttpCode(200)) =
  let buf = evhttp_request_get_output_buffer(req)
  assert buf != nil # should never be nil
  assert evbuffer_add(buf, str.cstring, str.len.csize_t) == 0
  evhttp_send_reply(req, code.cint, "", buf)
  return

proc onRequest(req: ptr evhttp_request, arg: pointer) {.cdecl.} =
  let uri = evhttp_request_get_uri(req)
  let path = if uri.len > 0: uri else: "/"
  let httpMethod = evhttp_request_get_command(req)
  case path
  of "/":
    respond("Hello, World!")
  else:
    respond("Not Found", HttpCode(404))

assert evhttp_bind_socket(httpServer, "0.0.0.0", uint16(8000)) == 0
evhttp_set_gencb(httpServer, onRequest, nil)
assert event_base_dispatch(eventBase) > -1, "Could not start event loop"

# cleanup after event loop ends, which it never does in this example
evhttp_free(httpServer)
event_base_free(eventBase)

Linking Libevent library

By default, the libevent Nim package will attempt to dynamically link against the system's Libevent library.

Dynamically linking:

{.passL:"-L/opt/local/lib -levent", passC:"-I /opt/local/include".}

Also, if you want to enable libevent thread support, you can link against the event_pthreads library instead:

{.passL:"-L/opt/local/lib -levent_pthreads", passC:"-I /opt/local/include".}

Statically linking is also supported, but requires you to have the static library available on your system. You can then enable static linking by adding the following flags to your project .nims file:

  --passL:"/opt/local/lib/libevent.a" # path to the static library
  --passC:"-I /opt/local/include" # path to the header files

❤ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors — All rights reserved.