Skip to content
a little java implementation of missing WebSocket form Android webview
Java JavaScript
Pull request Compare This branch is 4 commits ahead, 2 commits behind FreakDev:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Android HTML5 WebSocket

A Java library that makes a WebSocket object available for projects that uses a WebView (like PhoneGap project)


  • copy the java-src contents into the src folder of your project.
  • add import com.freakdev.phonegap.*; to the imports of your main java file
  • also add these two lines inside the onCreate function, after the loadUrl
   WebSocketFactory wsFactory = new WebSocketFactory(appView);
   appView.addJavascriptInterface(wsFactory, "WebSocketFactory");
  • copy websocket.js to your assets/www folder
  • edit your index.html to include the websocket.js
  • in your javascript, create a new WebSocket, and overload its methods 'onopen','onmessage','onerror','onclose';
var ws = new WebSocket("ws://");

ws.onopen = function()
    ws.send("Message to send");
    alert("Message is sent...");
ws.onerror = function (e) 
ws.onmessage = function (evt) 
    var received_msg =;
    alert("Message is received...");
ws.onclose = function()
    alert("Connection is closed..."); 
Something went wrong with that request. Please try again.