Search code examples
rubyajaxsinatra

How can I communicate to a Sinatra web app over a Unix socket?


I am writing a web application using Sinatra on an embedded device and need to be able to send and receive data over a Unix socket to the embedded code running on the same device. We will be most likely using JSON for the data format but I haven't found an example that really illustrates how to set this up and communicate with the web application.

I'm looking for both an example of the code that will listen on the socket and also an example that shows how to get that data into the Sinatra web page. I've seen bits and pieces of both but nothing that ties the concepts together.


Solution

  • I haven't done it personally but I know Event Machine supports working with unix socket. The advantage of event machine is that you can have code listening to the socket without 'interfering' with sinatra web server. So you can for example listen to socket for some statistics, store those statistics in memory and then return them to clients on HTTP requests.

    And if you are going for EventMachine I suggest using thin as your webserver which is implemented on top of it so you don't need to start it manually.