Subscribe/Publish messages to/from SocketIO/nodejs server from Java application? - java

Having a NodeJS + SocketIO server in javascript. Are there options to have a Java application to connect to that NodeJS/SocketIO server so that it can publish and subscribe messages?

I made something similar recently for teaching (node.js server, java client in publish/subscribe).
Actually, you should consider Faye : http://faye.jcoglan.com/
You can attach a websocket and/or bayeux server in a node.js instance using faye.
You can then connect any client that implement one or both of these protocol.
I successfully used it in the past for a lab with students in which a server is node.js publishing information, and a java client subscribe to faye using the cometd library (http://cometd.org/).
Source code of :
this server with faye but also traditional http server is located at : https://github.com/cgravier/WI-UCLab/blob/master/context-server-nodejs/simulateArduino.js
the java client using Cometd : https://github.com/cgravier/WI-UCLab/tree/master/context-client-java (sorry it is a library hell, it is designed for student that I was explicitly told were not familiar with maven (or equivalent), and it is 3h lab... anyway.)
Although the document is in French, I guess the figure at page 4 in https://github.com/cgravier/WI-UCLab/blob/master/lab%20document/context-awareness.pdf is self explanable.
HTH
fafhrd

I have used this one android-websockets which includes both SocketIO and Pure Websockets communication, but did not find it so stable, it had issues connecting to the server but this one socket.io-java-client seems like alright but have not used it.

Related

Framework for JAVA socket server application

I'm developing an Android app using a server side for computations and message handling. I need to work with push notifications, so I decided to go with Socket and ServerSocket.
For now, my server side is just a pure JAVA code that receive requests and open sockets accordingly. Is there any framework for my purpose? And how should I run my server side app on a remote server, should I create a runnable jar or is there any other way?
You can use Apache MINA, which is a socket framework with useful features like NIO, session management, SSL and other filter support.
Using a reliable framework means you spend less time writing boilerplate code.
Yes, you can create a runnable jar and create a bat/sh script to run on your server. Read this article for more information on packaging applications.

How do I use nanohttpd websockets from a client app?

I have implemented a websocket server using nanohttpd in java. I can access the websocket server from js in a web page. It works great.
However, now I'd like to create a java based client that will connect to the same server.
Does nanohttpd have a set of java classes to connect to the websocket server? In other words, the server is running in java but now I want a separate java client program to connect to it.
If so, what is the minimum java code to connect to the server?
If not, how would you suggest I connect to the websocket server in java?
There are plenty of third party Java Web Socket client implementations you can use. This is one of them.

Separated websocket for each connected client

I am planing to develop JavaScript client application that will connect to Java server using websocket. Server should handle many connected clients.
After some reading I found out websocket single thread. This is not good if I want to run databases query that can block everything for a while.
What I am thinking about is to opening separated websocket for each JavaScript client. One socket is listening for new connection and when connection is established creates some unique id. After that opens new websocket and send id to client using listener socket. When client received id close first socket and connect to new one.
What do you think, is it good solution? Maybe I am missing something?
Spring 4 gives you the chance to use a thread pool. The documentation is here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
You could use Akka to manage all the concurrency and thread management for you. Or you could use the Play Framework that already builds on Akka and that supports WebSocket quite nicely. With Play you can choose between Java and Scala on the server side.
You should use NodeJS on the server to handle the socket i/o. You can connect to it via your javascript client apps, and then make calls to your Java based API. NodeJS is non blocking (async) and you should be able to leverage your existing Javascripting skills to quickly build a Node app. You could even use a full MEAN stack to build the client/server app. http://meanjs.org/ or http://mean.io/#!/ are two popular places to start.

ProtoBuf from java application to web application

This is very new thing for me.
I am just wondering If we could send the ProtoBuf serialized data from java application to Web application (javascript) and de-serialize there. I am using TCP/IP connection in websocket to connect java application to javascript.
I have been looking at
https://github.com/dcodeIO/ProtoBuf.js/
but they are using node.js ,which is not in my case.
Thank you
ProtoBuf.js also runs in the browser. Basically, all you have to do is to connect your client to your Java server using a (binary) WebSocket and to send ProtoBuf packets back and forth.
Here is an example that decodes a message on the client side.

Porting java sockets client to HTML5 socket.io

I have a multi-player game that uses Java sockets, the server is a standard Java application and the client is a Java applet that runs in the web-browser.
Now since last Java's update (Java 7 update 51) all applets require code signing, so I would like to move way from the applet and rewrite the client in HTML5.
I've been looking into the socket.io and it seems quite easy, but I can't find any information on how to implement it into my server.
I would like to keep the server in Java, because it will be a lot of work to port it, so is there any libs that I could use on my server to make the communication possible between a java sockets server and a socket.io client, or what is the best approach? do I really need to port the entirely server?
Thanks.
The html5 WebSocket on which socket.io works is not equal to a "normal" C or Java socket. It implements its own protocol over TCP which includes handshakes and other stuff. To port your server you have to use a library maybe this helps you.
For more information on the WebSocket protocol see here.

Categories