0

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?

unor
  • 82,883
  • 20
  • 183
  • 315
mh283
  • 11
  • 3
  • I'll leave this just as a reference for the future [embedding a web server in Firefox OS](http://hacks.mozilla.org/2015/02/embedding-an-http-web-server-in-firefox-os/) – edoput Feb 10 '15 at 07:53
  • As the answer is already 2 month old, you could also consider using bluetooth instead of wifi. It could be a good alternative. – Loïc Faure-Lacroix May 18 '15 at 08:58

4 Answers4

1

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).

mh283
  • 11
  • 3
0

You should try httpd.js. This library is for FirefoxOS 2.0.

// create a server object
server = new HttpServer();

// configure /sdcard/public as document root
server.get("/", "/sdcard/public");

// launch on port 3000
server.start(3000);
kazhik
  • 546
  • 1
  • 5
  • 7
0

I don't think that you need a server for this task, you can do master-slave communication with WebRTC and handle the execution of the sound client side.

edoput
  • 1,118
  • 8
  • 15
  • How exactly would that work without the need of a server? As far as I can see, even [RTCPeerConnection](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection) needs a server for discoverability. – mh283 Jan 22 '15 at 08:11
  • Yes, you can use an external service that provide a server, like [peerjs](http://peerjs.com/), given that you provide an internet connection to connect the devices for the first time. – edoput Jan 22 '15 at 08:38
  • To rely on an internet connection and an external server is exactly what I wanted to avoid. – mh283 Jan 22 '15 at 10:10
  • WebRTC **is** p2p but need a signaling server to connect two clients first time, after that you could be on your isolated internet and still talk each other. What you are asking is local direct WiFi connection and http over that which is fairly complicated. Once you have a local WiFi provided by your master device you can talk to evey other slave over WebRTC without needing the internet AFAIK – edoput Jan 22 '15 at 10:29
0

I recently wrote an article on the Mozilla Hacks blog demonstrating how to implement this:

Embedding an HTTP Web Server in Firefox OS