Send continues data to client through websocket - java

I have a requirement to send the huge data through the websockets. My actual requirement is, the client communicates with my server for huge test data. My server will send the data assume the data size is 1GB. Its very hard to send this 1GB of data in a single response. So I choose the websockets. I am very new to this topic. I read about the websocets in multiple blogs and everyone gave a chat application example. But in my case client will ask once and my server needs to send the continues data. Is it possible to send the continues test data to the client with the websocets.? Can any one help me on this and if possible can you please provide an example.?
Note: I am using JAVA.
Thanks & Regards,
Amar.T

You already have the chat example. Use it. Try it on single computer. Probably you will need to create own protocol for sending/receiving data. Try to send by limited sizes blocks (for example 10 kb). So you will have 2 applications: client and server used websockets. I think that the main problem here is - What to do if connection was lost?

Related

make a request from java application to a web server url

I made a web-server that runs on an esp32(LAN) and I have made it possible to send information to the esp itself from the servers url, (example : 192.168.1.39/?userInput=123), the number 123 is what I want to send from the application depends on the user's input (I compiled it to a packet of 8bits) so max number is 255, the server has an XML and some basic UI for viewing the information passed back and forth, I wanna be able to send the so called packet to the server and it passing it to the esp32 with almost no delay, I used google firebase before but it has way too much delay for it to be usable, I tried using a WebView and loading the URL with the number from the packet, I ran out of ideas on how to approach this would love some advice :)
I tried searching other questions here on the site, asked friends/teachers, watched a few tutorials and asked chatGPT for help but nothing was helpful.
From reading your question it seems you are lost setting up server and client at the same time. Divide the tasks into chunks you can digest:
First, setup your ESP32 webserver. Follow a tutorial like https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ and test it using a normal web browser. It can be used to run GET requests easily, and the amount of data you need to transfer that should definitely be enough. Alternatively you can use curl to send client requests.
Next, develop your java client to send the appropriate request. You can test the behaviour using any standard webserver and check the logs.
Finally put the ESP32 url into your client and see whether they work together.

How does a chat app communicate with it's server?

I currently am developing a semi-simple chat app. I want the user to be able to communicate with one other person in a private chat. Just plain text will be sent. Currently my system calls php scripts from a webpage, passes in parameters and then parses the data returned. I have it so that the client sends the message, which calls a send message script on my webserver, the script then makes a message file on the webserver and then returns a success or failure back to the client. Then for the client to view this message, it would have to call a script that checks the server for a message file with a message for him. If it finds one, it sends the message back, if not, it sends a response about not having messages.
This technique works perfectly besides the fact that the client either would have to manually refresh to check to see if he had messages, or a background thread would have to refresh every few seconds. That would be fine, however that would use data if the user was on a mobile network. Not to mention the kind of resources a background loop would pull if it was refreshing at a speed that would be convenient.
So, I decided on a second idea, this would be a server programmed in Java, which would communicate over sockets. The user would send the message as a packet over the socket and the server would check to see who it was meant to go to. If the person is online, it passes the message along to that user. However this method requires a constant connection between the client and the server and that is unreliable because what if the user is in the car and data cuts out. Or some other situation where the connection gets severed. Then it would throw errors and have to reconnect.
Anyhow, my question is which technique is better. Or are they both terrible? If so, what is the correct way of doing this? Thanks in advance.
AngularJs and Ajax will be the perfect solution for you , try to learn
for actually real time messaging Use AngularJs
If the amount of data is very less ..say 20-25 messages per day...you can REST APIs on your server to transfer actual text messages and Google Cloud messaging for pushing notifications..Recently I followed this approach to develop private chat for one of my friend.

Restlet streaming data

I have this task that I'm undertaking where I would be reading data from a device and make it available over a web service. The data is read 4 times a second. I want the web clients to be have an open HTTP connection and get the device readings as a stream using chunked transfer as long as the client keeps the connection open.
As a proof of concept, I want to start with a service that constantly generates a random number, 4 times a second, wraps it in json and stream that to clients. I'm trying to model it loosely based on twitter streaming api.
I'm using restlet 2.1.2 to create that webservice but I'm not sure which Representation I should be using to achieve this. I tried searching for this but didn't find anything useful. Could someone point me in the right direction as to what I should be using and maybe some examples, perhaps.
Thanks
To achieve what you are trying to do, I'd use the WriterRepresentation (but see my answer to your other question), but I'm quite sure that you are going in the wrong architectural direction.
Indeed the following image from the documentation you linked
shows how even the Twitter streaming api is not intended to be connected by users, but by background processes that download messages in a store accessible by the HTTP. Users poll only the HTTP server, that reads the messages from the store and sends the back to the clients.
As a disconnected protocol, HTTP enable massive scalability that would not be possible otherwise. If each client establishes a persistent TCP connection backed by a dedicated server thread, you will rapidly exaust server resources! Moreover any HTTP proxy between the User Agent and the server could cause unexpected behaviours.
Thus, if you are bound to the HTTP protocol, the User Agent should poll. You can reduce the network load with headers like Last-Modified/If-Modified-Since or Etag/If-None-Match.
However, if you can adopt a different protocol, I strongly suggest to try a service bus over a connected TCP protocol.

Can web server send data to desktop application without request?

I have several PC's on each of them I set small swing application that get data with JSON request to one web server. Can I receive the data from web server without to send request to the web server, with other words can the Web server send the data without the Java application to ask for this?
If you have enough server resources
you can consider usage of websockets.
Every PC can open a socket to the server.
When you open the socket you need to send to the server, the pc's unique ID.
Then you need to store this ID in some database or file that will contain all online pc's and sockets .
Then the Server will be aware which pc's are online and which socket to use to communicate with this pc. After this you can send whatever information you need to this PC depending on your application.
This can be implemented in several ways. One common way would be to open a connection and do blocking read in the client application. On receiving something it will look like push from the server. Then you process the push and do another blocking read.
Another option would be doing regular checks if there is something for you on the web server. You set the retry interval frequent enough so it will look like real time push from your app point of view.
If you use HTTP i think the smartest way is to drop the realtime requirement and use a thread that polls the server every 5 seconds. Keeping a HTTP Connection open all time is expensive as it blocks a request processor thread and limits the amount of clients you can have.
You might also consider moving to something like a registration mechanism if you really need near-realtime updates which is often not the case. You would have to open a Server on the clients and have the server push the updates after clients registered their Address with the server.

Migrating Ajax web application to web socket

I think I'm just missing a little detail that is preventing me from seeing the whole picture.
I have a web application which use ajax request every x time to update client with new information or tasks.
I also have a long running process on the server which is a java computation engine. I would like this engine to send update to the client.
I am wondering how to migrate my web app to using websocket. Probably phpwebsocket or similar. Can my server 'decide' to send information to a specific client? It seems possible looking at the php-websocket.
Can my java backend long process use the websocket server to send notification to a specific client. How? well I can say that my java app could use a class that could send over websocket instead of http.
But how the websocket server knows to which client to send the 'info'. I am puzzle by all this. Any document that explain this in more details? It seems that the websocket could create an instance of my web application.
Thanks
Your server, which will have an arbitrary number of active client sockets, decides which ones to write to (possibly in response to input from the user).
phpwebsocket (which is still very rough around the edges) has a User class with $id, $socket (this is the underlying TCP socket), and $handshake fields. You could extend that class with additional metadata about the User (e.g. a computation identifier). Or you could use an array mapping from computation id to User.
Perhaps when Java computation n finishes, you can look up the socket associated with that computation, and write to its socket.

Categories