Socket.IO communication with backends in other languages

2,030 views
Skip to first unread message

David Benko

unread,
Nov 23, 2011, 7:49:53 PM11/23/11
to Socket.IO
I was looking for a solution to integrate a backend that I already
have made in Java with socket.io because its great browser
compatibility and I didnt want to rewrite all server in javascript.

So I tried all solutions around there:

https://github.com/ibdknox/socket.io-netty
https://github.com/benkay/java-socket.io.client
https://github.com/TooTallNate/Java-WebSocket
https://github.com/Ovea/Socket.IO-Java

And probably others. Couldn't get any of them working the way I wanted
(some of them are buggy with newer versions/drafts of socket.io or
websocket, jetty servlet sucks, etc).

So I made the simplest solution I could thought of: a simple tcp
socket, wich may work for every language that have socket api

I wanted to create a communication channel between browser and my Java
backend so this is how it works:

- Create a Node.JS TCP Server
- Java connects to node.js and send a string with the namespace name
it wants
- node.js then create a socket.io server with the namespace and start
forwarding all data from socket to all socket.io clients connected to
that namespace

A better approach would be replace sockets with something faster (like
http://www.zeromq.org/ - https://github.com/EhevuTov/netPeek)

Codes:

- node.js server: http://pastebin.com/Fv9g7sbF
- java code to send messages: http://pastebin.com/aNjuJjHa
- html sample code: http://pastebin.com/uazEgybE
* make sure to use the last socket.io (v0.8.7)

When I started this I had no js/nodejs/socket.io experience so as you
can see the server code is really messy and probably highly non
optimized.

Feel free to share opinions, sugestions, bugs and improvements

Thanks

Pavel Sakun

unread,
Nov 24, 2011, 9:28:44 AM11/24/11
to sock...@googlegroups.com
Hi David.

Didn't take a look at the code, but i would do the same as you described: node.js as a proxy between socket.io client and tcp java server. It's not complex and should work.

Pavel.
--
Best regards.
Pavel.

Su

unread,
Dec 5, 2011, 9:15:25 PM12/5/11
to Socket.IO
i tried to run your code under windows environment but failed.
Can you describe in more details how you get that to work please?
thank you.

On Nov 24, 8:49 am, David Benko <davidbe...@gmail.com> wrote:
> I was looking for a solution to integrate a backend that I already
> have made in Java with socket.io because its great browser
> compatibility and I didnt want to rewrite all server in javascript.
>
> So I tried all solutions around there:
>

> https://github.com/ibdknox/socket.io-nettyhttps://github.com/benkay/java-socket.io.clienthttps://github.com/TooTallNate/Java-WebSockethttps://github.com/Ovea/Socket.IO-Java


>
> And probably others. Couldn't get any of them working the way I wanted
> (some of them are buggy with newer versions/drafts of socket.io or
> websocket, jetty servlet sucks, etc).
>
> So I made the simplest solution I could thought of: a simple tcp
> socket, wich may work for every language that have socket api
>
> I wanted to create a communication channel between browser and my Java
> backend so this is how it works:
>
> - Create a Node.JS TCP Server
> - Java connects to node.js and send a string with the namespace name
> it wants
> - node.js then create a socket.io server with the namespace and start
> forwarding all data from socket to all socket.io clients connected to
> that namespace
>

> A better approach would be replace sockets with something faster (likehttp://www.zeromq.org/-https://github.com/EhevuTov/netPeek)

David Benko

unread,
Dec 6, 2011, 5:39:00 PM12/6/11
to sock...@googlegroups.com
First you need to install node and npm
http://nodejs.org/#download
http://npmjs.org/doc/README.html#Installing-on-Windows-Experimental
On the same folder of server.js type: npm install socket.io
Then: node server.js

Server is running, now you can run the java code
javac Client.java
java Client

It start sending data to the server and you can receive it in a browser by running that html example and checking javascript console (chrome has a built in js console or you can install firebug in firefox)

Good luck. 

2011/12/5 Su <suy...@gmail.com>
Reply all
Reply to author
Forward
0 new messages