<p>I'm exploring the possibility of writing an application in Erlang, but it would need to have a portion written in Cocoa (presumably Objective-C). I'd like the front-end and back-end to be able to communicate easily. How can this best be done?</p>

<p>I can think of using C ports and connected processes, but I think I'd like a reverse situation (the front-end starting and connecting to the back-end). There are named pipes (FIFOs), or I could use network communications over a TCP port or a named BSD socket. Does anyone have experience in this area?</p>

[{'answer_id': 37408, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 4, 'selected': True, 'text': "<p>One way would be to have the Erlang core of the application be a daemon that the Cocoa front-end communicates with over a Unix-domain socket using some simple protocol you devise.</p>\n\n<p>The use of a Unix-domain socket means that the Erlang daemon could be launched on-demand by <code>launchd</code> and the Cocoa front-end could find the path to the socket to use via an environment variable.  That makes the rendezvous between the app and the daemon trivial, and it also makes it straightforward to develop multiple front-ends (or possibly a framework that wraps communication with the daemon).</p>\n\n<p>The Mac OS X <code>launchd</code> system is really cool this way.  If you specify that a job should be launched on-demand via a secure Unix-domain socket, <code>launchd</code> will actually create the socket itself with appropriate permissions, and advertise its location via the environment variable named in the job's property list.  The job, when started, will actually be passed a file descriptor to the socket by <code>launchd</code> when it does a simple check-in.</p>\n\n<p>Ultimately this means that the entire process of the front-end opening the socket to communicate with the daemon, <code>launchd</code> launching the daemon, and the daemon responding to the communication can be secure, even if the front-end and the daemon run at different privilege levels.</p>\n"}, {'answer_id': 37683, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 1, 'selected': False, 'text': '<p>Usually when creating Cocoa applications that front UNIX commands or other headless programs you use an <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTask_Class/Reference/Reference.html" rel="nofollow noreferrer"><code>NSTask</code></a>:</p>\n<blockquote>\n<p>Using the NSTask class, your program can run another program as a subprocess and can monitor that program’s execution. An NSTask object creates a separate executable entity; it differs from NSThread in that it does not share memory space with the process that creates it.</p>\n<p>A task operates within an environment defined by the current values for several items: the current directory, standard input, standard output, standard error, and the values of any environment variables. By default, an NSTask object inherits its environment from the process that launches it. If there are any values that should be different for the task, for example, if the current directory should change, you must change the value before you launch the task. A task’s environment cannot be changed while it is running.</p>\n</blockquote>\n<p>You can communicate with the backend process by way of <code>stdin</code>/<code>stdout</code>/<code>stderr</code>. Bascially <code>NSTask</code> is a high-level wrapper around <code>exec</code> (or <code>fork</code> or <code>system</code>, I always forget the difference).</p>\n<p>As I understand it you don\'t want the Erland program to be a background daemon that runs continuously, but if you do, go with <a href="https://stackoverflow.com/questions/37381/whats-a-good-way-to-write-a-cocoa-front-end-to-an-erlang-application#37408">@Chris\'s</a> suggestion.</p>\n'}, {'answer_id': 54467, 'author': 'Jon Gretar', 'author_id': 5601, 'author_profile': 'https://Stackoverflow.com/users/5601', 'pm_score': 2, 'selected': False, 'text': '<p>One way is Theo\'s way with NSTask, NSPipe and NSFileHandle. You can start by looking at the code to CouchDBX <a href="http://couchprojects.googlecode.com/svn/trunk/unofficial-binary-releases/CouchDBX/" rel="nofollow noreferrer">http://couchprojects.googlecode.com/svn/trunk/unofficial-binary-releases/CouchDBX/</a></p>\n\n<p>Ports are possible but not nice at all. </p>\n\n<p>Is there some reason for why this communication can\'t simply be handled with mochiweb and json communication?</p>\n'}, {'answer_id': 54488, 'author': 'argv0', 'author_id': 5595, 'author_profile': 'https://Stackoverflow.com/users/5595', 'pm_score': 1, 'selected': False, 'text': '<p>The NSTask and Unix domain socket approaches are both great suggestions.  Something to keep an eye on is an Erlang FFI implementation that\'s in the works:</p>\n\n<p><a href="http://muvara.org/crs4/erlang/ffi/" rel="nofollow noreferrer"><a href="http://muvara.org/crs4/erlang/ffi" rel="nofollow noreferrer">http://muvara.org/crs4/erlang/ffi</a></a></p>\n'}, {'answer_id': 238047, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>erl_call should be usable from an NSTask. I use it from a Textmate command and it is very fast. Combining erl_call with an OTP gen_server would let you keep a persistent backend state with relative ease. See my post on erl_call at my blog for more details.</p>\n'}, {'answer_id': 3769189, 'author': 'chad', 'author_id': 455025, 'author_profile': 'https://Stackoverflow.com/users/455025', 'pm_score': 1, 'selected': False, 'text': '<p>Using NSTask you may also consider using <a href="http://amath.colorado.edu/pub/mac/programs/" rel="nofollow noreferrer">PseudoTTY.app</a> (which allows interactive communication)!</p>\n\n<p>Another sample code of interest could be BigSQL, a PostgreSQL client that enables the user to send SQL to a server and display the result.</p>\n\n<pre><code>open -a Safari http://web.archive.org/web/20080324145441/http://www.bignerdranch.com/applications.shtml\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/37381', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3948/']