Search code examples
webserverfirefox-os

Use Firefox OS as web server


For an art project, I'd like to have multiple distributed devices that can output sound. Firefox OS devices seem optimal. They bring the necessary hardware and I know HTML and JS very well. But I also need a control web server.

From my understanding, a Firefox OS device can act as an WiFi access point ("Share Internet"). However, it cannot act as a small web server for other devices that join the network – without any internet connection. The APIs for native apps seem just not to be powerful enough.

But maybe I am mistaken (I would like to be). So, is a Firefox OS device able to run as a small web server?


Solution

  • httpd.js did not work out-of-the-box for me. But it brought me on the right track. I then found this and after a little bit of tweaking and updating of the code, I got a super-simple server solution.

    function startListen(){
      console.log("Initializing server");
      var socketServer = navigator.mozTCPSocket.listen(8080);
    
      socketServer.onconnect = function(conn){
        console.log("connected", conn, conn.ondata);
        conn.ondata = function(ev){
          console.log("Got request: ", ev);   
          conn.send("Ok. Got client on port " + conn.port);
          conn.close();
        };
        conn.onclose = function(ev){
          console.log("Client left:", ev);
        }
      };
      socketServer.onerror = function(ev){
        console.log("Failed to start: ", ev);
      };
    }
    startListen();
    

    The tcp-socket permission is needed.

    With this code, I was able to start this in the Firefox OS simulator, direct my browser to open http://localhost:8080 and get an answer and logs in the console.

    PS. This also works on a real device. Unfortunately, a separate access point is needed. While Firefox OS can work as a hotspot itself, it can neither be client or server in that mode (outgoing connections are not routed properly and incoming connections are refused).