Giter VIP home page Giter VIP logo

fbzmq's Introduction

fbzmq

Build Status

fbzmq provides a framework for writing services in C++ while leveraging the awesomeness of libzmq (message passing semantics). At a high level it provides

  • Lightweight C++ wrapper over libzmq which leverages newer C++ constructs and stricter type checking. Most notably it provides the ability to send/receive thrift objects as messages over wire without worrying about wire encoding/decoding protocols.
  • Powerful Async Framework with EventLoop, Timeouts, SignalHandler and more to enable developers to write asynchronous applications efficiently.
  • Suite of monitoring tools that make it easy to add logging and counters to your service.

Examples

Here is a simple example demonstrating some powerful abstractions of fbzmq which makes writing asynchronous applications easier on top of libzmq

// Create context
fbzmq::Context context{1};

// Create eventloop
fbzmq::ZmqEventLoop evl;

// Create thrift serializer
apache::thrift::CompactSerializer serializer;

// Create REP and PUB server sockets
fbzmq::Socket<ZMQ_REP, ZMQ_SERVER> reqSocket;
fbzmq::Socket<ZMQ_PUB, ZMQ_SERVER> pubSocket;
reqSocket.bind("tcp://[::1]:12345");
pubSocket.bind("tcp://[::1]:12346");

// Attach callback on reqSocket
evl.addSocket(RawZmqSocketPtr{*reqSocket}, ZMQ_POLLIN, [&](int evts) noexcept {
  // It must be POLLIN event
  CHECK(evts | ZMQ_POLLIN);

  // Read request
  auto request = reqSocket.recvThriftObj<thrift::Request>(serializer);

  // Send response
  thrift::Response response;
  response.request = request;
  response.success = true;
  reqSocket.sendThriftObj(response, serializer);
});

// Create periodic timeout to send publications and schedule it every 10s
auto timeout = fbzmq::ZmqTimeout::make(&evl, [&]() noexcept {
  thrift::User user;
  user.name = "TestPerson";
  user.email = "[email protected]";
  pubSocket.sendThriftObj(nodeData, serializer);
});
timeout->scheduleTimeout(std::chrono::seconds(10), true /* periodic */);

// Let the magic begin
evl.run()

Requirements

We have tried fbzmq on Ubuntu-14.04, Ubuntu-16.04 and CentOS-7. This should work on all Linux based platforms without any issues.

  • Compiler supporting C++14 or higher
  • libzmq-4.0.6 or greater

Building fbzmq

Repo Directory Structure

At the top level of this repo are the build and fbzmq directories. Under the former is a tool, fbcode_builder, that contains scripts for generating a docker context to build the project. The fbzmq directory contains the source for the project.

Dependencies

  • cmake
  • gflags
  • gtest
  • libsodium
  • libzmq
  • zstd
  • folly
  • fbthrift

One Step Build - Ubuntu-16.04

We've provided a script, build/build_fbzmq.sh, well tested on Ubuntu-16.04, to install all necessary dependencies, compile fbzmq and install C++ binaries as well as python tools. Please modify the script as needed for your platform. Also, note that some library dependencies require a newer version than provided by the default package manager on the system and hence we are compiling them from source instead of installing via the package manager. Please see the script for those instances and the required versions.

Build using Docker

Learn more here.

Build Steps

// Step into `build` directory
cd build

//  Install dependencies and fbzmq
sudo bash ./build_fbzmq.sh

make test

Installing fbzmq

fbzmq builds a static library and install steps installs library as well all header files to /usr/local/lib/ and /usr/local/include/ (under fbzmq sub directory)

sudo make install

Build and Install python libraries

cd fbzmq/fbzmq/py
python setup.py build
sudo python setup.py install

How fbzmq works

zmq/* are a straight forward C++ wrappers over raw libzmq objects (like zmq_msg_t, zmq_ctx_t, etc) in C. ZmqEventLoop is an event loop which allows you to attach different event callbacks on sockets as well as schedule timeouts. Internally it maintains a dynamic poll-list which is updated only when new socket/fd is added/removed and uses zmq_poll APIs for polling the list. It also maintains internal heap of timeouts ordered on their expiry times.

Full documentation

To understand how to use library, take a look at examples/ directory. All of our code is very well documented and refer to appropriate header files for up to date and detailed documentation of various APIs/functionalities.

License

fbzmq is MIT-licensed.

fbzmq's People

Contributors

aeckert avatar ajanthanasogamoorthy avatar avalonalex avatar bkoray avatar chadaustin avatar fanzeyi avatar jesboat avatar joe-el-khoury avatar joeloser avatar jstrizich avatar kevin-vigor avatar lnicco avatar lukaspiatkowski avatar mcallahan avatar mohamedbassem avatar neheb avatar orvid avatar pkaush avatar plapukhov avatar saifhhasan avatar shri-khare avatar simpkins avatar snarkmaster avatar udippant avatar vgao1996 avatar vitaut avatar wez avatar yfeldblum avatar yns88 avatar zpao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.