How to communicate between or send data from Javascript to Java through Sockets?

Vivek Vijayan
4 min readSep 11, 2022

Intercom between Javascript and Java — Websocket

Recently I have come across a scenario where I need my Java Desktop application needs a trigger function from Javascript (chrome extension) on the webpage I open on my local machine. but I get to know that this is possible only by making a web API server in Java and making the website (chrome extension) respond/request through API.

But making a server running in Java Swing application doesn't make sense, as it is a show stopper, until the server closes the swing app can’t respond to my clicks or any other parallel activity.

Making the server run in another process is also not effective as the main achievement is missing here (getting web data from Js to Java).

So I thought of developing a WebSocket by which two entities can talk to each other through a socket and don’t need a 3-way handshake every time.

Web socket communication pattern

So what I did is, I created a Socket Server in my Java Swing application that would run in a separate thread, and be ready to accept a WS Response from any app through a port number that I have assigned.

Another proper that I have encountered here is, that the latest version of Java have deprecated the ReadLine() function from DataInputStream class.

ReadUTF() function reads the Modified UTF-8 encoded string only, which can be written only by another method called WriteUTF() in Java itself. Here I want my Javascript to communicate with Java which accepts only Modified UTF-8 encoded string, in which Javascript doesn’t have “mutf8” encoding. There is some node library called “mutf8” that could convert your UTF8 to MUTF8 format but still doesn't work.

Then Finally I figured out an alternate way to achieve my task, I used a Scanner class to get the data from the getInputStream() of the Socket.

(Not using ReadUTF() method)

Java Server Socket creation — Port: 9999

As I created such functionality in Java, I need a Socket Client on the Javascript side. You have many libraries to perform socket calls in node js like socket.io, node: net, etc.

Since we are about to make the call from the browser, we cannot have such libraries on the web, as we use in node. These libraries will support only in node, not in a web browser.

as net libraries won't support browsers, but in node env.

So the only way for us to use the traditional WebSocket call

let socket = new WebSocket("ws://127.0.0.1:9999");

Great !!! 🎊 , we have now made a connection from Javascript to Java. but how do we send data???

so by using a WebSocket call, we can send the data to the Java Server. by using the method called “Send”

socket.send("Hello from JavaScript");

Here, another problem is the encoding mismatch by the send method used by JavaScript, which Java doesn’t understand, and Java returns some garbage text once it receives a call in that port.

So the final solution is, to send the data in the URL itself, as a parameter. so the Java prints out the parameter that we pass through the WebSocket call.

let socket = new WebSocket("ws://127.0.0.1:9999?message=HelloFromJs");

once you design your Javascript to append your data to the URL to pass, you can make a proper communication to the Java Server.

Once the socket connection is established, Java Server will print the below

GET ?message=HelloFromJs HTTP/1.1

Using such string, you can trim or split to extract the data you need to proceed further.

Wow 🥳, we made communication from Javascript to Java using WebSockets.

Conclusion

Thanks for reading, I hope this blog helps. Please do follow me for more updates 😇

Stay connected

LinkedIn * Github * Medium

--

--

Vivek Vijayan

I’m a Computer Science Engineer, IoT Expert and Full stack developer, helping programmers in getting their projects fulfilled