Is there a C++ implementation of Node.js Net API? - c++

Node.js has a very good and well thought Net API.
I wonder is there a good C++ only implementation of that API as for example LuaNode do for Lua?

Take a look at node.native - attempt to implement api similar to node.js core lib, but with c++11 (and evented IO is also based on libuv)

There is nothing very similar that I know of.
However there are several reactor frameworks out there which give the same event queue driven environment. For example boost::asio provides an event queue that makes callbacks to handle network events, timers, and arbiatary events that you push onto the event queue.
It's largely the same idea, used in the same way. Howver it's nowhere near as simple as node.js to get started with, and does provide any non-blocking functions other than the basics I said above.
It does provide you with the environment to build your own system though. It's an excellent library, but probably rather lower level than you are looking for. There are other simiar libraries such as ACE and parts of the POCO c++ libraries too, but again, they are lower level than node.js with much less library support.
edit:
I've not looked at it too much but how about this https://github.com/joyent/libuv . This is a library that is used to implement some of the node.js features in a cross platform way. Maybe it's possible to use some of it for what you need?

Boost.Asio is conceptually very similar to Node.js. The primary difference being Asio is implemented as a library and Node.js is a language construct. Asio therefore exposes the event queue, requiring some initial setup to post callback handlers, and eventually a blocking call to start the event loop (io_service.run()).
If you're looking for a pure C++ API similar to Node.js, Boost.Asio is definitely the way to go. It is the de-facto networking library for many C++ applications. It's also discussed heavily on SO in the boost-asio tag.

I'm quite sure you could embed a Javascript engine into your program
v8 (building guide)
SpiderMonkey (building guide)
Actually tying that to your C code needs tinkering with the eval functions of both, but I think I remember seeing sample programs doing that for both engines

Related

Comparing Boost.Asio, libunifex, liburing, and CppCoro

I am trying to understand asynchronous models in C++. I am investigating 4 libraries that purport to deal with asynchronous I/O:
liburing (C version, C++ version): provides an interface for io_uring. The C++ version uses coroutines.
libunifex: implements the C++ sender/receiver async programming model
Boost.Asio: library for asynchronous network and low-level I/O programming
CppCoro: provides a large set of general-purpose primitives for making use of coroutines
These libraries have an enormous amount of overlap.
Execution context and io_uring
The future of asynchronous I/O will likely use io_uring on Linux machines. liburing exists to provide an interface for io_uring with io_service. However, libunifex also provides io_uring_context. These both explicitly use io_uring but have usage similar to Boost.Asio's io_context and CppCoro's io_service.
Can liburing's io_service, libunifex's io_uring_context Boost.Asio's io_context, and CppCoro's io_service all be used together? If my code included all four of these libraries, would I have 1 execution context from each?
Functionality: opening files
This section contains an example of how CppCoro asynchronously opens a file. I believe this template class would be used in Boost.Asio for asynchronous file access. libunifex has an io_uring_text.cpp doc that includes asynchronous writes to files. Obviously liburing exists for asynchronously writing files.
Should I only use the io_uring specific libraries for file access?
Functionality: networking
Everyone knows that Boost.Asio provides networking functionality. CppCoro does too. The liburing GitHub includes an example echo server that uses low-level C code.
Linbunifex does not appear to include direct networking functionality. I presume it is meant to be combined with some other networking library. Would I combine it with one of the above three libraries to give an old library a sender-receiver interface? How would I do that?
Interface: coroutines
I'm guessing that coroutines are meant to be glue that allows these libraries to exist together. Boost.Asio is compatible with coroutines. liburing4cpp and libunifex have coroutine compatibility as a central feature. CppCoro, on the other hand, is essentially a coroutine library.
Does it at all make sense to talk about using coroutines as glue? Could I say "I will use library A to implement the async function and library B to handle the output of the async function"?
My Questions
Would you expect to see someone use all 4 of these libraries simultaneously? Are any of these libraries meant to be used on top of any of the others?
In cases where functionality overlaps, how do these libraries compare on performance? Is Boost.Asio falling behind new asynchronous models? Is Boost.Asio staying ahead by integrating the most recent asynchronous models? liburing4cpp included a low-level echo server that isn't much comparable to the modern interface of Boost.Asio. How do applications based on low-level io_uring usage compare to applications built using Boost.Asio?
I believe that senders/receivers in libunifex is primarily meant as an interface. Should I use it as an interface to other libraries, for example Boost.Asio?
Look at asio-grpc, I think they had almost all of your comparisons in examples

MPI or Asio for wide-area plugin-based message passing

I'm writing a distributed system wherein each node interfaces with local applications via some RESTful API, supports extensions and runtime customization, etc. It's a bit like an Enterprise Service Bus over the wide area, but with a lot else going on which is not related to the question at hand.
I've read a little about both MPI and Asio - originally I was set on Asio, but then I found MPI, and now again I'm thinking Asio is the better solution for me. MPI seems to provide a lot I don't need and a higher level of abstraction than I want - for example, I only need point-to-point communication, and it is important to me to be in control of when and what data is transmitted. (e.g. I have already designed a packet structure that I would conform to ideally)
So my primary question: it is worth it to start from a lower level with Asio, or should I try to graft MPI onto what i'm looking for? Further, are there 'skeleton applications' available which use MPI or Asio which would aid development? (Actually I am 100% new to C++.. ;) Or, does it make sense to use them in tandem?
For more perspective, maybe it's worth noting that I already have implemented the bulk of this project in Perl using Perl Object Environment, which itself is just an asynchronous event system with a ton of networking libraries.
Also, if it makes a difference, I would ideally use threads for this.
Honestly though I have not used Boost at all yet, as I hinted above, so any input is appreciated.
I should start by saying that I know nothing about Asio, but from the 2 minute scan of the website and the description of your problem, it sounds like while either would probably work for you, Asio might be simpler. MPI is really designed less for general purpose network communication and more for running applications where the set of processes and applications is a little more static. While it provides a client/server style interface if desired, it's not the main focus of the library.
MPI is also more difficult to use if you already have a packet structure designed. MPI is great for abstracting away the need to worry about packets, location of processes, etc. but if you've already taken all of that into account in you application, you have already done the hard work.
There have been at least one other discussion of Asio vs. MPI that you can take a look at (For distributed applications, which to use, ASIO vs. MPI?) to get more opinions too.

Scalable server framework in C++

I am looking to write a server application in C++ that is meant to handle tens of thousands of clients simultaneously. It should run under Windows and Linux. I have been looking around for frameworks and libraries and have come across Boost Asio, which seems like a highly mature and widely used alternative. I just have trouble wrapping my head around strands/thread pools, mainly because of the millions of templates. My background is mainly in C, so am not really used to the template mess that Boost in general seems to be full of. I tried to find someone to develop a relatively thin wrapper around Boost Asio that would take care of the threading/synchronization aspect using strands, bind and the like, but have been unable to find someone yet who can do it within my budget (2 or 300 US dollars).
Can any of you recommend any other libraries that scale as well as Boost Asio (e.g. with IOCP on Windows and epoll on Linux etc), or a source where I might find skilled Boost developers looking for smaller freelance jobs?
Thanks very much in advance for any help.
Kind regards,
Philip Bennefall
Best 4 choices i know
I really like zeromq.. but libuv seems interesting.. (libev and libevent are very nice too)
zeromq
libevent (as said)
libev
libuv (Its purpose is to abstract
IOCP on windows and libev on Unix systems and it is node.js network layer)
ACE is the framework you are looking for. Even boost Asio is just an implementation of Proactor pattern, which was introduced by Douglas C. Schmidt. He is best known as the author of POSA Vol.2 and the creator of ACE framework.
The Boost.Asio library offers side-by-side support for synchronous
and asynchronous operations ... based on the Proactor design pattern
[POSA2].
Although it is a cross-platform C++ network framework and uses template,
just simple template is used. (or not at all)
My background is mainly in C, too, and I don't like Boost's massive template-programming style. However, ACE wasn't like that.
Try libevent on for size. Its whole raison d'etre is to address the C10K problem. I'd say it's probably more lightweight than boost.
Try Pulsar Server Framework. Main benefit is it is built over libuv network library (used by node.js) that uses asynchronous I/O based on event loops.
It’s perfectly scalable. You can just go adding servers as your user base increases.
It is designed to work with server farm.
Highly configurable and easy to use
Currently it has been built for Windows x64 server.

Portable lightweight C++ sockets wrapper

I really thought this would be easier to find...
I need a portable c++ sockets wrapper. I'm planning to use it for a windows server application and a client that will be running on a embedded device running ulinux (or something similar). I would use Boost but I need it to be lightweight and easy to add to the embedded device project.
Also I would like it to be a "higher level" wrapper... so it starts a background thread to read data and informs be over a callback...
Any ideas?
I'd suggest Boost.Asio. Despite it's name, you are not forced to use asynchronous I/O. You could use synchronous I/O and threads, as your question implies.
Boost.Asio is a cross-platform C++
library for network and low-level I/O
programming that provides developers
with a consistent asynchronous model
using a modern C++ approach.
Just learn to use the socket API directly. You can then easily wrap it yourself. It's not that hard, and you can get started with Beej's excellent guide. As Beej says:
The sockets API, though started by the
Berkeley folk, has been ported to many
many platforms, including Unix, Linux,
and even Windows.
In his guide he details the very small addition you need to do to get the same API in Windows and *nix systems.
Once you've learned, wrap it yourself if you're so inclined. Then you can control exactly how "lightweight" you want it.
If you really don't like Boost asio then you might like the sockets support in dlib. It is simpler in the sense that it uses traditional blocking IO and threads rather than asio's asynchronous proactor pattern. For example, it makes it easy to make a threaded TCP server that reads and writes from the iostreams. See this example for instance. Or you can just make a simple iosockstream if not acting as a server.
I know this is old, but there is a very nice and simple implementation in below location which I'm using for personal use. Had implemented my own wrapper a while back but lost the code and found this one online which is much better than mine:
http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/
Take a look at ENet http://enet.bespin.org/ it is very lightweight and portable and works on top of UDP, with optional support for reliable packets. It is easy to use, the API is low-level and with little performance overhead. You have a high degree of control over the memory management, which could be good if networking is a bottleneck for you and the malloc/new implementation you use performs badly under multithreading.
It would not be that hard to implement your high level thread “optimally”, since there is optional support for blocking receive and the library is a “library” and not a framework therefore you are the decision maker instead of the library.
Perhaps you can have a look at http://www.pt-framework.org/
Old question, but for C++, BSD style synchronous sockets this is about as minimal baggage wrapper as you can find
http://code.google.com/p/ting/source/browse/trunk/src/ting/net/
It does come with exceptions. You could make a bit more lightweight one as a header-only template library, and maybe make exceptions optional, but that would change the API a bit
POCO network classes are quite similar, but do require more dependencies from other parts of the Poco lib
I'm personally creating my own AsIO wrapper for both TCP and Serial sockets, and I started by reviewing the following tutorial:
https://www.gamedev.net/blogs/blog/950-they-dont-teach-this-stuff-in-school/
and
https://objectcomputing.com/resources/publications/mnb/multi-platform-serial-interfacing-using-boost-a-gps-sensor-and-opendds-part-i/
I found the first one very useful and simple to understand.
C++CSP2
Used it loved it. Stable and powerful

Recommend crossplatform C++ UI and networking libraries

Things to take into consideration:
- easy to use
- fast
- use underlying OS as much as feasable (like wxWidgets for UI)
Ones I am leaning towards are wxWidgets for UI and Boost for networking - how do they compare to others?
I hear good things about qt for GUI
Qt is a cross-platform application and
UI framework. Using Qt, you can write
web-enabled applications once and
deploy them across desktop, mobile and
embedded operating systems without
rewriting the source code
I've had good look with wxWidgets on the front end and boost::asio on the network end.
wxWidgets does have network classes built in, but you hit the wall quickly on them, and there's one or two big limitations. If you want to stay in the wx world, there's a package called wxCurl which is a fine package (I used it in the early days) that wraps libCurl with some wxWidgets idomatic C++.
In a previous project of mine (a network/file transfer heavy project) we ended up going with boost::asio, which had the advantage of not being all that hard of an API, easier-seeming to set up that libCRUL (although that may have gotten better, that was been several years now), and gives us a very generic networking core (boost can compile anywhere, even command line apps)
For GUI I would strongly recommend using Qt. It is very powerful GUI framework that requires writing very few lines of code. It has very nice and easy to use model of signals and slots.
wxWidgets IMHO too modeled after MFC which has very bad model.
Networking: I would suggest go for Boost.Asio very powerful and nice. However if you
want to integrate networking to GUI main loop you may try to use Qt classes for that, however I have no experience with them.
I've used XVT historically, which has been used commercially by thousands of companies.
Both Qt or wxWidgets can do networking even if it's not their first goal.
For more network centric libraries, apart from boost::asio, you can check ACE (Adaptative Communication
Environment ) or POCO
Comparisons between these libraries have already been discussed on stackoverflow.
boost::asio seems to be very well written, and has a very clean API -- I am still trying to learn how well it is for shared-nothing multithreaded TCP/IP.
Your other choices might be Poco, or ACE. Poco's socket abstraction is quite naive --i.e., it only allows the Poco way of doing things. I've never heard anything good about ACE.
edit: Hmm, I'm re-examining ACE and its making more sense to me now (after having written a few networking apps) -- it might be suitable for my needs compared to ASIO. However, it is more than likely overkill for you. If my peers find out about this, I will be shunned till the end of time.
We have had good success using wxWidgets with boost::asio, both recommended for desktop-server development.
For GUI, I can recommend QT
For Networking ACE (Adaptive Communication Environment) or boost::asio.