How to redirect after some time in Play Framework? - SCALA - scala

I have a following problem. When someone enters my index page, I would like them to see it for a while (let's say 2 seconds) and then be redirected to another page. What is the best way to do it in Play Framework? (I tried using WS.url().get as well as calling a method which return Ok - both without any result)

You'll need to use JavaScript/HTML for that. Play will allow you to do server side redirects, which will be instantaneous, for a delay it has to be done client side.

Related

Changing the start page in MDriven

I have understood that using the "Index view model" to display data is not the the preferred way to show data at start-up. I therefore hoped to execute a periodic action in Index to redirect to the correct start page. But I do not see how I do this in practice? Has anyone used "Index" as start page with good results or has any one managed to execute the redirect action?
Since the index is rendered with MVC - it must have a client side timer to execute any periodic action. I do not think there is one added currently - I think there was at one point but the ambition to keep the MVC pages clean removed it.

Showing timer with WebSockets

I have an application (Laravel + MongoDB running on Nginx) where I pull some data from the database and render it on the screen. The application focusses on multiple real life objects. Once an object is turned on (is_on equals to true in the database), the timer on the screen needs to start ticking. Once the object is turned off (is_on equals to false in the database) the clock stops ticking and resets to 0. The format of the clock is HH:MM:SS. So it shows how long the real life object is turned on.
My problem is that I don't really now how to save/implement such timer. When the user request the page, I pull the necessary data from the database. If I also save the timer in the database, you have to make a query every second which is very bad practice.
I remembered something about WebSockets and tried to look into them. I actually managed to build a basic Hello World chat application, but don't really know how to implement this in my project. There is no place for it in the database (because of the queries), so I don't really know where to save that timer on the server. I'm also doubting if WebSockets are the way to go.
So are WebSockets the way to go and if it is, can you guys point me in the right direction on how to implement this? If not, can you advise me what I should do?
Thanks in advance!
From your question:
I understand that the objects you print in the screen are modified by
users in the application, and your aim is to live forward those
modifications to other active client instances of your application.
In that case, as you mention, I would point you to websockets. They are a great way to feed information directly to the client, so the client receives the update signals and modify the interface, with no need of user action.
In order to implement the logic to notify the client, I recommend using a push approach, but this is really depending on what kind of clients you'd like to notify, since the push world is still a bit tricky.
Further readings for this websocket based push implementation:
Question about Push Flags:
Difference between push and urgent flags in TCP
If your client runs in browser or mobile this question is nice to read:
How to send push notification to web browser?
Also html5 websockets:
http://www.websocket.org/aboutwebsocket.html
As a sidenote:
A nice architecture for client-server live communication is based on node.js together with socket.io library offering good performance and not really complex implementation if you know what you do.

Play 2.0 - Push current state of execution to page

So I currently have an application independent of Play which may take a long time in its execution.
I want to put a UI on top of it using Play where the application may be invoked and to display some of the details of the execution inside of the application to the user. I would like the page to be updated automatically as the execution proceeds e.g. if a variable in the application increments this would be reflected on the page.
I'm not sure where to begin with this - do I need to split the application up into models + controllers ? Or do I just need to have code in a controller to instantiate the classes I have already coded and call the methods I need ?
What about constantly showing the execution state on the page?
Any resources I should know about/read ? Code examples?
Thanks
You may have already done so, but a good starting point is to create a skeleton Play application using the play new command, while referring the creating a new application section. You will have "views" (HTML template pages) and one controller (in Application.scala). You could add more controllers, but as you will have just a single page that should suffice.
You can add jars from your app (if it's a JVM app) to the lib directory of your Play application. From this: "Or do I just need to have code in a controller to instantiate the classes I have already coded and call the methods I need?" it sounds like you would be happy to have your app run in the process of the Jetty + Play server. Check out the Global object for starting your app at process startup.
Check out the section on comet sockets for sending updates from the Play app to the browser. You'll need a bit of Javascript in the web page.
Do you want to have this application running outside of play, perhaps on another server? Can you modify the application, or is this 3rd party software?
If so, you have to have some way to send data back and forth between your play front end and your application. You can either have your application expose a websocket, and then your play front end and your application can push data back and forth to each other. You can then have your client page have a websocket open to you play front end, and then play can push the updates to the client. If your application can't support a websocket, you could also expose some URLs on your front end for the application to POST to. You can then use some sort of message bus or database mechanism (RabbitMQ, redis, Mongo capped collection, or even just a shared Queue object) so that the front end websocket can get those updates and send them to the client.

how to show eclipse console in my jsp page?

I have a method in action page, it will take more time to execute so I have write some description on console window. But now i want to show the description on text area in jsp page. how to show like as console.
Two possible solutions are the "execAndWait" interceptor and using ajax.
Execute and Wait will be the easier to implement but will present less functionality...
http://struts.apache.org/2.3.4.1/docs/execute-and-wait-interceptor.html
Ajax can provide continuous feedback of the state of the request (although managing that feedback is entirely up to you and it will typically require you to think in terms of services, one page will typically make use of multiple services).
For this you will not have to worry about the amount of time it takes your action to return because it is being called asynchronously. You could even make a queue showing how many request you've sent and pop them off as they are received.
To implement ajax services it is easiest to start with json and for that see the struts2-json-plugin http://struts.apache.org/2.2.3/docs/json-plugin.html

Server Real Time on Website

I would like to grab the time (something like): 16:08 from the server - and display it on the website real time. (Like, each minute do the update, and display the new time.
I'm not sure however, what would be a good way for accomplish this nor if there is any nice plugin that I should be aware of. :D
Can I have some insights about the possibilities here?
I'm using PHP and I can use Jquery as library.
Thanks a lot,
MEM
Use Javascript and set a timer for every minute. Have the timer call a function that uses Ajax to call the correct page on the time server which gets the time and then have the Javascript replace the existing time.
You could also just get the actual time from the server when you load the page, then use Javascript to increment the minute/hour section appropriately by using a timer that 'ticks' every minute.
If the problem is just to display the time you can use Date.getTime() javascript function.
But if you want to get the time from the server and display it to the client you can use an ajax request using JQuery.