Socket to receive data and push to client browser in a web application - sockets

There is a SocketServer which I need to connect, receive data and then push it back to clients (web browser).
I just need to confirm the approach. I will create a Socket connection which will keep listening for message inside a ServletContextListener. The message will be then be stored in ServletContext so that it's available to push it back to client browser.
Should I create socket connection inside ServletContextLister? Or is there any other way to listen to socket?
For pushing data back to client, I don't want to have a bi-directional communication. So should I go for SSE or Html5 WebSockets? Any java framework supporting SSE or WebSocket along with client side library?
Thanks

I implemented this and its working fine. For server I went with Jetty 8 and used jetty's WebSocket.
Briefly how I implemented is, for listening to messages from SocketServer I implemented ServletContextListener which initializes Socket which will consume the messages.
When a user hits a url I add the WebSocket connection to a Collection and store that collection as ServletContext attribute. This WebSocket list is then accessed by the Socket which iterates and sends messages where whenever messages arrives. The message is then pushed back to all the connected clients.

Related

If I send some data then immediately close the socket, will the other end receive the data?

For .NET framework System.Net.Sockets.Socket, if I send some data then immediately close the socket, will the other end receive the data?
If not, is there a way for me to close the socket only after the other end receives the data?
WebSocket over HTTP works by establishing a TCP/IP connection, which requires the clients to sends an upgrade header in the payload to the server, and if the server can establish the create a WebSocket connection, it will reply with a HTTP 101 Switching Protocols response. Once the connection is established successfully, you can send data back and forth.
Interesting enough, requesting to close the connection also requires you to send a frame, which means you are also sending data to the server. Not even that, to properly close the connection, the sever also needs to respond with the same closing frame, once that happens the TCP connection is terminated and you can no longer send data.
What that means in your case is the server would receive the data you had sent before sending the close request frame.
You can read more about the details of frame, http headers, etc in the protocol RFC https://tools.ietf.org/html/rfc6455

How can a Bayeux Client stop receiving messages from Bayeux Server? Is there a way to block the channel between client and server at the client side?

At the server, CometD provides a MaxQueueListener hook to drop messages but if the Bayeux Client wants to stop receiving messages from a server without disconnecting , can it achieve that?
A BayeuxClient receives message from the server only for the channels it is subscribed to.
For a BayeuxClient to stop receiving messages from the server is enough to unsubscribe from all channels it subscribed to.
The BayeuxClient will still receive meta messages on meta channels that are part of the Bayeux protocol, but no application message will be delivered by the server.

Ejabberd with Stream management (XEP-198) not using offline message hook

We are developing an app with a chat feature. We have an ejabberd (15.02) configured to use mod_offline_post to use the offline message hook and forward all messages for offline clients to an url of our own which then forwards to the GCM.
However as we are developing an app, we also need XEP-198 (stream management) enabled to handle connection loss. This is working fine in itself. Streams are created and resumed, messages are acknowledged.
The problem is, that the jabber is waiting for a stream to resume and is not forwarding any offline messages to the offline message hook and thus to our mod and post url. It is storing them in its offline storage of course and they get delivered when the recipient resumes its stream.
Is there any way to configure the jabber to call the offline message hook while ejabberd_c2s:fsm_next_state:2517 Waiting for resumption of stream for... ?
PS: We use smack on the client side to provide stream management
In my understanding the behaviour of ejabberd is correct from the XMPP specification point of view. It is doing the right thing and should not in that case forward message to the offline store, because you are not offline technically.
It is just not the right place to place your push processing.

How to receive an incoming message in Socket Programming iOS

I'm developing an App where I'm able to send/recieve messages to/from PHP server using socket, I'm referring Ray's tutorial for Socket programming.
But now I want to send to send messages to another user who is using the App, I have his IP Address and Port no, So I can send message to the user but How will that user recieve the message, because he not connected to my port. How will he notify that there is an incoming message for him?
I'm doing socket programming for very first time.

Sending message from server to client without managing sockets

Im building a chat app for iPhone and im using NSURLConnection with HTTP Post and Get to send messages from client to server. From what i understand this is a non-persist connection, because NSURLConnection open and close connection for each HTTP request, am i right??
In my server i just response to this messages.
My main problem is how to send a message from client A to client B?
client A sends the message to the server with HTTP POST, but how can i send the message from the server to client B if im not keeping a persists connection to client B?
You could make use of push notifications to inform the client that it has an unread message.