Hello,
I am working on ChatBot application written in HTML and JavaScript.
For some user inputs from ChatBot user, I have to communicate with C++ code to accomplish their task.
What is the best way to communicate between the JavaScript and C++?
Any good libraries would be helpful.
I think the answer i’m going to come to on this is “You don’t. You have C++ implement a method of speaking directly to the chat server without an intermediate layer between them.”
But just to flesh the idea out a little bit… you’re talking about a browser running on the same machine as the C++ script, open and using javascript in a webpage?
The “best” way the way I would see it, would be to build a middleware layer in something like Node.js that implements a websocket connection and a REST fallback, the passes that on to the C++ app and listens for the response. This is the “best” imo, because it’s easy. It takes almost nothing to put this together. This is how most applications that leverage C++ processing work. Node is not a requirement, it can he switched out for anything that is good at listening to websockets and http requests.
The most performant way would be to build the Websocket/REST in C++ and have the chat bot listen for them directly. Doing this will probably be more work than it’s worth.
You’ll need to do it in some way to talk to the web layer (Node or whatever), but if you use a web layer you won’t need to implement fallbacks and can use whatever communication protocol is easiest to build in C++ or even better… have them communicate through a message queue like RabbitMQ.
It’s hard to give a best answer without more context, but here are a few options.
If you’re on the backend and need to communicate with C++ code, consider a node addon. Or if you’re on the frontend, consider WebAssembly. Alternatively, make your C++ program return an HTTP response and serve it as a CGI program, then either your frontend or backend can use it like any other web service.