Socket connection is not closed, then navigate back to Launchpad - sapui5

I have an Fiori Elements application, which consumes an Socket connection over the object:
sap/ui/core/ws/WebSocket
If an message is send to the Frontend, the App shows an PopUp.
Problem:
User uses APP
User navigates to Launchpad
Socket connection is not closed
Message is send by Socket
PopUp is shown in Launchpad
How can I achieve that no PopUp is displayed?

Solution was simple, implement onExit method of Controller would do the trick
onExit() {
this.socket.close();
}

In case if a WebSocket object is created in the Component.js, be sure that the WebSocket is initialized in the Component init() function and call the WebSocket close() method in the Component exit() function.

Related

How to receive publish/message event from Sails server using Socket.io swift client

Currently i am building app using Sails server as a backend and iOS Swift as a client.
I am able to emit from iOS side to create user and receive data from server side, but i am not able to get PubSub methods(message, publishAdd, Update,etc) provided by sails.
Here is my server side code
User.subscribe(req, [userdata.id]);
Now when there is any update occurred on above User recored then i need to receive event like
socket.on("userupdate") { data in
print("user 1 ------------>", socket)
}
But i am not receiving any event on client side.
Any help will be appreciated :)
Thanks
Actually i am missing watch on server side also need to do changes on client side like below
socket.onAny({ data in
print("ANy event =====> \(data)" )
})
socket.on("user", callback: {
data,ack in
print("call back called \(data) \(ack)")
})
socket.OnAny user to trace which event being called while there is any event for publish, update,etc
Here are server side changes i have done
User.subscribe(req, newUser, ['message']);
User.watch(req);
User.publishCreate(newUser);
Hope this will be useful for developer looking to implemented socket with Sails server.

Custom route SailsJS and Socket

O would like to use a custom method with socket on SailsJS.
new: function(req, res){
Talk.create({message: "text"}).exec(function created(err,created){
Talk.publishCreate(created);
res.send(created);
});
}
On client side I am doing exactly as described here: http://beta.sailsjs.org/#/documentation/reference/websockets/resourceful-pubsub/publishCreate.html
publishCreate are not sending updates to client side. Is there something I am missing?
Most likely the problem is that you have not used the .watch() method to subscribe to create messages, as stated in the documentation you linked to:
The default implementation of publishCreate only publishes messages to
the firehose, and to sockets subscribed to the model class using the
watch method
In the latest releases of Sails, the sails.config.blueprints.autoWatch property defaults to true, meaning that a socket will automatically start listening for "create" messages whenever it does a "find" call, as in io.sockets.get('/talk'). Otherwise, you need to subscribe the socket manually in your controller using Talk.watch(req);.

How To Resume Atmosphere Connection When User Cancel Closing Browser Window

Our application is using atmosphere as the comet implementation,we want to implement the functionality that user can choose to stay within the site after browser window is closing by prompting the user "Are you sure" dialogbox .
To be specific, the problem originate from the fact that when user is trying to close browser window, when the confirm dialogbox is showing, the disconnect event is fired to atmosphere server no matter what user is going to choose.
We are using atmosphere for GWT, and we tried using the resumeOnBroadcast on resource option ,but with no luck.
And we also tried to intercept the closing event before it fired, also failed.
So can somebody help, thx a lot.
Take a look at the onClose(AtmosphereResponse) request handler.
var socket = $.atmosphere;
var request = new $.atmosphere.AtmosphereRequest();
request.onClose(response){
//handle the close here
}
var subSocket = socket.subscribe(request);
subSocket.push(data);
This is documented here. This is on the client side.

Play 2.0 Comet using a persistant connection

I am using play 2.0 for a realtime web application that connects to a backend socket in order to listen for data on that stream.
The following example does what I want but I don't know how to disconnect the socket if the clients webpage is closed or page has changed.
def comet = Action {
val out = Enumerator.imperative[String]()
val socketClient = new SocketClientModel("localhost", "testclient",
Option("username"), "password", out)
socketClient.listen
Ok.stream(out &> Comet(callback = "console.log"))
}
The problem that I am having is figuring out how to call socketClient.disconnect when the page has been closed or changed. Currently when I close the browser session I can still see the connection is established and data is being received on the server side.
A hacky solution could be for you to have a ping call in the other direction (client to server):
You setup a timer that will destroy the connection if it hasn't been canceled before 10 seconds (you can always setup a longer period):
var timer;
def comet = Action {
timer = Akka.system.scheduler.scheduleOnce(10 seconds) {
socketClient.disconnect
}
...
}
You setup an Action that receives pings and setup a timer in the javascript code to send requests to that route every 9 seconds:
def keepAlive = Action {
cancel.cancel
timer = Akka.system.scheduler.scheduleOnce(10 seconds) {
socketClient.disconnect
}
}
When the user goes to another page, or cancel the connection to the comet Action, then it should also cancel the ping timer on the client side (if the user change page, this should be automatic, but you will have to have some health check on the comet connection if you stay in the same page, etc.). When the client stops pinging your action, the timer will eventually kill the socket.
It's really ugly as you need to use a var that is shared between your two actions and it's not really thread safe. It also would only be able to support one client. But the code example you have given is the same I guess. You would need to track which session has open which socket to support multiple client. You would loose stateless behaviour though.
A cleaner solution would be to use a small actor system to encapsulate this behaviour:
- you have a SocketActor that, when it receives a Start message, opens a socket and pushes things on a PushEnumerator, it keeps doing this as long as it doesn't receive a Stop message.
- you also have a KeepAliveActor that will after a given amount of time send a Stop message to the SocketActor, unless it has received a KeepAlive message before (so, what's going on here).
When you receive a comet connection, you spawn two new actors (you should have a manager actor to hide the two actors and relay the messages, etc.) and keep a ref to them linked to the session. The keepalive action would then fetch the ref for the right session and send KeepAlive messages to the actor.
The SocketActor would, when it receive a Stop message close the socket and destroy the actors that were linked to him.
It's a bit more involved coding, so I am not including snippets, but it would be a simple AKKA system, so you should be able to set it up by checking out the AKKA tutorials if you are not yet used to it.

grails+spring-security-facebook listen to login success event

im a newbie both in spring security and spring-security-facebook and in an app that we are building we have to couple them.Everything is working well i will need to know the way to listen to the facebook login success event. is there some one that already did a stuff that impose him to catch the facebook event success?? I need that because in the begining of the app (before adding the spring -security-Facebook plugin ); we have a special behaviour attached to the "grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent" event (configured in the config.groovy file) and we have to execute the same special behaviour when the user connect with facebook account. Is there an event (extending springsecurity kinds of events) that we have to listen to?
any idea ??
Ps: when searching for solution we found a way to catch the FB js Events and work around to reach what we want as result but we would as possible to want to not go that way ....
The problem with it that current Spring Security Facebook authenticates user on each request (by listening to FB cookie, transparently), so you'll get this event on each request. Probably it's not what you want, right? It the same as having a filter on each request.
Btw, you can handle situation when user login into your app first time, but implementing onCreate(user, token) or afterCreate(user, token) in service FacebookAuthService (you have to create such service at this case)
Example:
File grails-app/services/FacebookAuthService.groovy:
class FacebookAuthService {
void afterCreate(def user, def token) {
log.info("New user!") //or any code there
}
}