Docs Menu
Docs Home
/ / /
C Driver

Async C Driver: Public Preview

On this page

  • What is amongoc?
  • Why Go Asynchronous?
  • Getting Started with amongoc
  • Feedback: Your Voice Matters

We are excited to share the release of a new prototype that could reshape how C developers interact with MongoDB: the amongoc library. This experimental driver leverages the power of asynchronous programming to offer an efficient, non-blocking interface for MongoDB operations. Although still in public preview and not yet ready for production, your feedback will play a crucial role in shaping the future direction of this project.

amongoc, short for "asynchronous MongoDB C driver," is designed for developers who need a performant, asynchronous client library for MongoDB. It implements a selected subset of MongoDB driver APIs to provide a sneak peek into what a C-based asynchronous client might look like. By evaluating this prototype, developers can help us gather critical insights that could influence the development of a production-ready solution in the near future.

The motivation for building amongoc is rooted in performance optimization and providing high concurrency. Asynchronous programming decouples the initiation of a task from its continuation, addressing the inefficiencies of synchronous operations, particularly I/O, which are inherently slow and can block other processes. Asynchronous programming allows applications to remain responsive while waiting for I/O operations to complete, making it ideal for high-performance, network-intensive use cases. amongoc will also facilitate the integration of MongoDB into asynchronous frameworks like Drogon. Notably, it has also been the top requested feature for the C driver by users.

To explore amongoc, visit the GitHub repository and access our comprehensive Documentation. Our resources include:

The following example is an application that initializes an asynchronous event loop and attempts to establish a connection to a local MongoDB server using the amongoc library:

#include <amongoc/amongoc.h> // Make all APIs visible
#include <stdio.h>
#include <stdlib.h>
amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result);
int main(void)
{
amongoc_loop loop;
amongoc_status status = amongoc_default_loop_init(&loop);
amongoc_if_error(status, msg)
{
fprintf(stderr, "Failed to prepare the event loop: %s\n", msg);
return 2;
}
// Initiate a connection
amongoc_emitter em = amongoc_client_new(&loop, "mongodb://localhost:27017");
// Set the continuation
em = amongoc_then(em, &on_connect);
// Run the program
amongoc_detach_start(em);
amongoc_default_loop_run(&loop);
// Clean up
amongoc_default_loop_destroy(&loop);
return 0;
}
amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result)
{
// We aren't using the userdata for this example.
(void)userdata;
// Check for an error
amongoc_if_error(*status, msg)
{
fprintf(stderr, "Error while connecting to server: %s\n", msg);
}
else
{
printf("Successfully connected!\n");
amongoc_client *client;
amongoc_box_take(client, result);
// `client` now stores a valid client. We don't do anything else, so just delete it:
amongoc_client_delete(client);
}
amongoc_box_destroy(result);
return amongoc_nil;
}

This public preview release is focused on gathering user feedback to inform the architectural decisions for a production-ready version of amongoc. We invite you to share your thoughts and suggestions in GitHub discussions and issues. We are especially interested in your opinions about the interface, and the build and runtime requirements. Our goal is to collect as much user input as possible to align with the needs and expectations of our user community. Your feedback is invaluable, and we eagerly look forward to hearing from you and seeing what you'll create with amongoc!

Back

MongoDB C Driver