2

I need to find a way to communicate between Firefox for Android (fennec) and my Android service. One of the solution was to communicate using WebSockets Code:

var ws = new WebSocket("ws://localhost:8887/");
ws.onopen = function() {
  console.log("SENDING");
  ws.send("Hello messsage");
} 

06-16 13:14:10.099: I/Gecko(8055): Message: ReferenceError: WebSocket is not defined

Is WebSocket supported in fennec addons?

nmaier
  • 32,336
  • 5
  • 63
  • 78
Misha
  • 433
  • 4
  • 10

1 Answers1

1

WebSockets are available on Firefox for Android, however the usual WebSocket constructor is not available in bootstrap.js (Sandbox) or js code modules as these do not have a DOM window attached but are standalone JS.

However, there are two ways you can still create web sockets:

  1. Use new Services.appShell.hiddenDOMWindow.WebSocket(...). (Though, I remember there were talks at some point to remove the hidden window on Android?!)
  2. Use the somewhat lower-level XPCOM Component nsIWebSocketChannel and implement nsIWebSocketListener.

BTW, you might want to check out Remotely debugging Firefox for Android.

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • 2
    I think Fennec would honor `new Services.wm.getMostRecentWindow('navigator:browser').WebSocket` as well. – paa Jun 16 '14 at 14:47
  • @paa That will work too (for now), but you'll need some extra parentheses to make clear that `getMostRecentWindow` is not the constructor, but WebSocket, so `new (Services.wm.getMostRecentWindow('navigator:browser').WebSocket)(...)` – nmaier Jun 16 '14 at 15:39
  • @paa thank you so much, that was really helpful, it saved us so much effort! – Amit Dugar Jul 24 '15 at 12:13