Question

I'm trying to run a computation-heavy program that creates an image on a website. Is it possible to compute in C++ and have an output stream that connects to an input stream in Node.js to display an image, or alternatively, stream is to the user and display it through JavaScript?

Also, I'm using Monte-Carlo Sampling so the stream would be binary.

Was it helpful?

Solution

Short answer: yes.

A stream in the context of software is simply a sequence of bytes. What those bytes represent is up to you: it could be ASCII text, Unicode characters, a JPEG image, serialized object, anything your heart desires. The important thing is that the producer and consumer of the stream agree on the format.

Furthermore, a stream is not specific to two programs talking to each other. You can use streams inside the same program so two modules can talk to each other in a loosely coupled way.

What you are really looking for is the idea of sockets. A socket is typically used for IPC, or inter-process communication. A high level overview of the process looks like this:

  1. Program A opens a socket to a specific location, normally an IP address and port.
  2. Program B is listening on that IP address and port, and opens the socket on the other end.
  3. Both programs open streams for that socket. Program A can write bytes to its output stream, which show up in Program B's input stream after crossing over the socket. The reverse is also true, allowing bidirectional communication.
  4. When communication is done, one side closes the socket connection.

(If you think about it, this sounds a lot like loading a web page in your browser. This is not coincidental.)

Note that the language, architecture, and physical location of the two programs are not specified. That is the beauty of sockets: they are very low level and simply do not care about those details.

Normally you will use a library to deal with sockets and streams. Some languages have such facilities built-in to the standard libraries, some do not. Regardless, most popular languages will have something available because this is a very common task asked of modern computer systems.

Licensed under: CC-BY-SA with attribution
scroll top