Skip to content

openpeeps/libevent-nim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

👑 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

Cool projects using LibEvent

Here are some cool Nim projects that use the LibEvent library:

  • MeowMail - A SMTP server implementation in Nim using LibEvent.
  • Supranim - A high-performance web framework in Nim using LibEvent.
  • RTMP Server/Client - A RTMP server & client implementation in Nim using LibEvent.
  • Groovebox - Badass CLI app for streaming to Twitch, Youtube, any RTMP servers and 🧊 Icecast-compatible servers

❤ Contributions & Support

🎩 License

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