Communication between javascript client and java server - javascript

I want to create a js code that permit from every browser where it's running to open socket with a java server and send receive data(string) between us, i googled a lot i have found technology like node.js or DWR but they can still help me.

Related

mqtt client in html page

I want to have a web page that can publish and subscribe to a mqtt broker.
I also want communicate to the broker using the 1883 port - not a websoctets.
is this possible - does anyone have any working examples ..
I'm a real novice , and admit i dont fully understand the difference between nodejs and javascript.
I have a html page made up , it has a checkbox setup, the checkbox is set to switch an led on an esp8266 through a mqtt broker. after i load the page - the switch works... but i'd like as soon as i load the web page that it would chek the status of the led and reflect that on the check box. ie: when I load the page I can see if the led is on or off.
I have a working nodejs script that can publish a request for a status to the led, and subscribes for the repsonce .. and it sucessfully recieves a responce..
is there any way i can build this script into my html page such that when it loads -it gets the responce from the script and sets the checkbox accordinaly ?
I see some examples of java mqtt html clients but these seem to only work with websockets.
In my project I'm not sure what listner ports I am allowed to use for the websockets ( if any) so would prefer to stick with mqtt port 1883.
all thoughts welcome please ?
thanks
If you want to connect to a MQTT broker from with a web page you MUST use MQTT over Websockets. The Javascript sandbox in the browser will not allow you to do it any other way.
You will need to use a MQTT broker that supports MQTT over Websockets and you will most likely need to configure it to do so. It will need to be on a separate port to 1883 as this is traditionally used for native MQTT.
You can use either the Paho JavaScript library (https://www.eclipse.org/paho/index.php?page=clients/js/index.php) or the MQTT.js library (https://www.npmjs.com/package/mqtt) in the browser. Both libraries come with examples.
We will not just provide examples here on Stack Overflow, try with examples supplied by the libraries and if you get stuck, open a new question and show what you tried and explain what is not working and we will help you fix it.

How to trigger js event with Java?

I am building a java chatting web application.(Server and Client in one project)
follow is my condition
Springframework 4.2.3
JSP
Maven Project which converted from a Dynamic Web Project
Unable to use node.js
So User scenario in my head is
User enters some text and press Send
Ajax call to deliver messages to server
Server checks the users who are currently connected(from Session maybe)
Server calls other users script to append new message
I am quite confusing with step4. Is it possible that Java calls DOM event trigger?
How could a client get a new message event from server?
Thanks. :D
P.S.
These days majority of chatting servers are event-driven. Is it possible to build an event driven chatting server with Java?
Your solution is formed as if there is no WebSocket technology available to you. WebSocket is implemented to solve real time messaging issues. It pushes message to the destination.
But if to stick wih your method following is meaningful.
You need someplace to keep incoming messages such as database or session.setAttribute [bad idea]. Then use some ajax call loop on the clients machine to ask for a new incoming message from server.
Probably your server will slow down due to incoming flood of GET requests from multiple users.
to Your last question in post scriptum: yes, I use tomcat websocket api.jar in my projects. There is well written documentation on apache.org
As I mentioned, learn WebSocket if your users are not using old internet explorer browsers. There are bunch of tutorials on it...

Delphi server with JavaScript client

Hi I was wondering how one would go about setting up a delphi server that can interact with a javascript client. I am developing a game with this tool : www.scirra.com
It is called construct 2 and it uses javascript. It has a plugin to use socket.io and websockets so either one would be great.
So basicly I want to develop a delphi server that would receive messages from the one javascript client and broadcast it to the other javascript client. If the user wanted to host a game he would run the delphi application and then join the game and wait for other players to join.
To sum up;
(A) Is it possible to use websockets in delphi to communicate with javascript.
(B) if so would someone please make a simple demo.
Thanks for your time
There are some WebSocket client and server implementations available (see WebSocket server implementations for Delphi).
By design, client-side JavaScript (in the browser) can use the WebSocket protocol to communicate with the server. The Delphi WebSocket server implementations then can handle the requests and push data back to the client just like any other WebSocket server library. However, I do not know anything about the code quality or Delphi version compatibility of all these libraries so some additional research is required.
Regarding the Demo: most libraries surely include some demo HTML with JavaScript / WebSocket communication. Simply download it and open it in your favorite text editor ;)
p.s. as I can see socket.io not only supports WebSocket but also long polling so basically you can use any HTTP server library for Delphi to write the server side logic. See:
How can I update HTML pages dynamically with Indy HTTP server using jQuery and "Long Polling"?
How to: update HTML pages dynamically using jQuery and “Long Polling”

How to connect a Javascript client with Red5 server?

Anyone know how to connect a JavaScript client with Red5 Server?
I need to do this without flash, flash player or Java Virtual machine.
Thank you very much
You can write for example a servlet and invoke a REST call via JavaScript to the servlet. The servlet can then invoke a method and broadcast the message to all connected flash clients via RTMP.
Another possibility would be for example you integrate any other possibility to receive REST calls. Red5 is a regular Java Application server, normally deployed on Tomcat (or Jetty). So you could for example integrate Axis2 and have a full featured REST framework to handle JavaScript calls and forward those messages to the RTMP clients.
Sebastian

Client-side browser socket communications for web app - Best method?

Sorry for the cryptic title, struggling to summarise my problem in a single line...
I wish to deploy an online, hosted website to serve a series of remote terminals which will be equipped with Chip & Pin payment card readers (aka pinpads). The pinpads are driven by some software on the PC within the terminal which is written by a 3rd party. The integration methods supported by this software are either text file based or socket based with a "request" and "response" workflow.
I have successfully carried out similar integrations in the past using client side VB Script to instantiate client side COM objects which communicate via socket connection with the local 3rd party software but this approach ties me to Windows and I would prefer to keep my options open.
My web server will be Ruby On Rails based and I intend to use HTML5 and CSS3 to provide a rich experience on the payment terminals and wondered if I can use web sockets for client side communications? From what I understand, this is not what they are designed for and so I think the answer is no.
So, what are my options? Can i use client side JavaScript to carry out socket communications or is this prevented by browser security measures? From the browser's perspective it would be communicating with a specific numbered port on "localhost"
If socket comms is not possible, can I use JavaScript to create client side text files to integrate that way?
Or am I stuck with VB script and local COM objects?
Any suggestions would be most welcome and please let me know if you need clarification on any aspect of my question.
Kind regards,
Craig.
I don't think you can write a text file with JavaScript. And you can't put arbitrary bytes on a socket either. I don't completely understand you scenario. It sounds like you have Ruby sunning on a server and JavaScript and this third party pinpad thing running on a client. And you need the two client entities to be able to talk to each other. Could you have the browser communicate with your Ruby server (using one of many web technologies) and then have your Ruby server relay the data back to the pinpad socket. Or is the pinpad only a local socket?
The only type of socket-based connection you are allowed to open on a standard web page that runs javascript is an HTTP socket. You'll have a lot more freedom to use sockets if you develop a browser plugin, which is written in javascript. Firefox, I know, supports sockets in extensions.
https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsISocketProvider

Categories