Web Socket - Spring : Confirm of message received - java

I am sending a message through WebSocket with Spring from Tomcat Server to a SockJSClient with the method:
WebSocketSession.sendMessage(WebSocketMessage<?> message)
and I would like to know when the message has been received (eventually with complementary information, for example whether the logic on client successfully processed), then go for next message.
This is an Activity diagram that explains the use case.
How can I receive confirmation of reception or result from client?

As Erwin pointed, you can adopt some higher protocol providing such feature like STOMP. However, if you are afraid to adopt it only for that feature, you can implement that feature by yourself.
The first thing is to give each message id to identify each message, type to recognize the purpose of each message, data to transport a message's content and reply which is a flag to see whether or not ACK is required and to use a format like JSON to serialize/deserialize an object containing these data into/from WebSocket message.
When sending a message, it creates an object by issuing a new id for that message, setting type to message and data to given message and reply to true if ACK is required or false if not. And it serializes it into JSON and sends it as a WebSocket message. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L88-L110
When receiving a message, it deserializes JSON to the above object. If reply is true, it sends a special message whose type is reply setting data to id of that message. Then, the counterpart can confirm that its counterpart has received a message whose id is id. - https://github.com/cettia/cettia-protocol/blob/1.0.0-Alpha1/lib/server.js#L46-L76
The above links point similar implementation in Cettia which is a real-time web application framework I wrote. Though that implementation is a little bit complex as it is designed to allow for user to handle callbacks with result, you are likely to get the basic idea.
API implemented by that link looks like the following.
A server or client which requires a result of event processing.
// Using Java server with lambda
socket.send("foo", "bar", result -> /* resolved */, reason -> /* rejected */);
The corresponding client or server which has a responsibility to submit the result.
// Using JavaScript client with arrow functions
socket.on("foo", (data, reply) => {
// data is 'bar'
// 'reply.resolve(result)' if it successes
// 'reply.reject(reason)' if it fails
});

Related

How to differentiate between the automatic reply and the normal mail received

I am working on receiving mails in my springboot application. In order to fetch and store the receive mails. I am using imap mail listener. There are two types of mails which I am storing. One is multipart payload type and the other is string payload type.
After receiving mail I am trying to send an auto-generated mails using java mail.
The issue which I am facing is worst case scenario of generating auto-reply from my application i.e infinite loop.
Can someone help ow can I differentiate between a normal mail received and auto-reply received in my mail box and generate auto-replies from my system only for those mails which are not auto-reply type.
It would be nice if explained via code for headers check. I came across through few headers like x-Autosubmitted. But they are returning null if I am trying to print the values in console.
The auto-submmitted markers are described below that you may find helpful:
auto-generated - Indicates that a message was generated by an automatic process, and is not a direct response to another message.
auto-replied - Indicates that a message was automatically generated as a direct response to another message.
auto-notified - Indicates that a message was generated by a Sieve notification system.
no - Indicates that a message was NOT automatically generated, but was created by a human. It is the equivalent to the absence of an Auto-Submitted header altogether.
The RFC 2822 states the following:
Though optional, every message SHOULD have a "Message-ID:" field.
Furthermore, reply messages SHOULD have "In-Reply-To:"
So, you may check for the "In-Reply-To:" value in the header.
Also you may add your own value to the outgoing email, so you may distinguish between an automatically generated reply from your system and manually created.

Android - PubNub - Pass the UUID or Username of a user with each sent message

HelloI've built an Android application that uses PubNub to create a chat channel between each user. I would like to be able to identify which users have sent which messages. Currently the login of my app is handled by Parse so each user has a unique username. I found some documentation and example code where rather than sending just the message string, an object was set up that contained the UUID and message string as two different objects that could then be extracted on the subscribe side but from what I could tell this was only in the PubNub javascript code not the Java code for Android.Right now i'm thinking that the only way for me to do this is to attach the UUID/username to the beginning of my message string with a special character to seperate the UUID and the message and then split it up and read it in on the subscribe side. For example String message = "uuidhere_messagehere";. Is this the correct way to approach this or is there a better, more convenient way of doing this?thanks
Correct - PubNub does not inject anything into your messages so you will need to include the sender id within each message that is published. Here's is a simple example of a JSON message you might publish:
{'sender_id':'user_333', 'msg':'this is my msg to you-hoo-hoo'}
Of course, the JSON message can have any key/value pairs you require.

How to send Notification Message with gcm-server.jar

I am using the gcm-server.jar to send gcm Messages from Server because its easy to use. (http://www.java2s.com/Code/Jar/g/Downloadgcmserverjar.htm).
Messages are sent with this code. This works fine:
Message msg = new Message.Builder().addData("message", message).build();
Sender sender = new Sender();
Result result = sender.send(msg, token, 5);
...
How can I send a GCM Message with Notification Payload like in this JSON:
{"to":"token" ,
"notification":{
"sound":"default",
"badge":"1",
"title":"this is the title",
"body":"this is the body"}}
You need to have a server set up where you parse these messages in JSON format so that GCM can process it accordingly.
Usually this depends on what server technology you are using. Also, you might want to check the validity of the library you referenced as GCM framework has been updated substantially.
Here's a good place to start.
And another good tutorial here. (although this one is older too but gives you understanding of server side implementation)
Hope this helps!

Missing body in JMS / websocket message

I'm trying to understand why serialized objects are being stripped out when received.
Here is the general layout :
1. Produce JMS messages and send to a topic on an external ActiveMQ broker. The code is based off the Spring boot JMS message and can be viewed at http://bit.ly/QECQ21 . In my example, I generate various types of messages (text , POJO, Map)
2. I have 2 JMS consumers that subscribe to this topic.
a. One is a java client. This client correctly identifies all the various message types.
b. The 2nd client is a javascript client based on websockets + stomp.js . This correctly identifies text messages but not messages that contain POJOs generated by my producer.
Here is the sample output from the web client (source at http://bit.ly/Od0noF ) .
What am I doing wrong? Something wrong with the the addressing or something else in the code? Something about the way I'm using (or misusing) STOMP? I had to use "tcp://localhost:61616" in the Application.java class to correctly contact the broker.
Thanks
MESSAGE priority:4 persistent:true subscription:sub-0 expires:0 timestamp:1396418227090 destination:/topic/greetings message-id:ID:blackbox-53461-1396418226684-1:1:2:1:1 content-length:4 ping
Body:ping
MESSAGE priority:4 persistent:true subscription:sub-0 expires:0 timestamp:1396418227117 destination:/topic/greetings message-id:ID:blackbox-53461-1396418226684-1:1:2:1:2
Body: ======> missing body
MESSAGE priority:4 persistent:true subscription:sub-0 expires:0 timestamp:1396418227122 destination:/topic/greetings message-id:ID:blackbox-53461-1396418226684-1:1:2:1:3
Body: ======> missing body
MESSAGE priority:4 persistent:true subscription:sub-0 expires:0 timestamp:1396418227125 destination:/topic/greetings message-id:ID:blackbox-53461-1396418226684-1:1:2:1:4 content-length:5 close
Body:close
No answers yet so I'll add my findings. STOMP is a text based protocol and as such the body needs to be converted to a text format (such as JSON). In my case, this can be done using the spring websockets framework (which internally uses Jackson) or by explicitly coding the object into JSON using Jackson and them transmitting as a text message

MINA: Is finishDecode where I send a response message?

In using Apache MINA, I'm sending a login request from the client, which is interpreted on the server via LoginRequestDecoder (implements org.apache.mina.filter.codec.demux.MessageDecoder).
I now want to send a response (LoginResponse) that includes a success/failure code. Should I be sending the response from the LoginRequestDecoder's finishDecode() method, or is there a better place for it that I'm overlooking?
What I needed to do was make my IoHandler of type DemuxingIoHandler. Within its constructuor, I had to make multiple calls to addReceivedMessageHandler and addSentMessageHandler. This allows the code behind DemuxingIoHandler to automatically respond based upon the type of message received.

Categories

Resources