Real Time Background Service - javascript

I am now developing a mobile app using ionic framework and it's using larvel 4 REST API to do CRUD operations with MySql database. Based on the app requirement, the mobile app needs to call backend service everytime in order to complete certain process.
I am using Angular Resource to call those APIs and i am calling them every 3 secs using Javascript setinterval function. However, the app is working, I don't really feel that it's a good practice to do because it's a heavy task for both server and client.
Could you guys please guide me how I should solve this kind of situation and i really appreciate your help. Especially, which kind of tools I should setup and what are they? Thank you.

setInterval isn't a best practice to receive new data from your API server/DB because the server could get flooded when too many users access your web site. However there is a prettier solution: WebSockets. With websockets you receive notifications realtime. I am not a PHP developer so I don't know what WebSocket libraries are used for Laravel/PHP development. But on the AngularJS part I can recommend this: https://github.com/gdi2290/angular-websocket
A WebSocket example (non-related): http://jsfiddle.net/EAVvQ/24/
Hope this helped.
Cheers!

Related

HTTP Request Mobile Javascript

Hey everyone so I have a question, can I have an endpoint api in my Mobile application?
For example I have a server that would do stuff with data and then I would send a post request to my mobile application letting it know new data had came in. How would I go about that? Is that even possible?
My solution I came across was to use firebase api since I remember It has a watcher. So I can easily change some data inside the firebase database by using my server. The mobile application will have the firebase watcher and see that something in the FB database got changed and it will proceed to react to it.
Without using firebase. If I were to send a get request to my server from my mobile application every second(as a watcher) is that bad practice? Or is that pretty much what firebase's watcher is doing?
I know that when you deploy a web application you can have a backend inside the directory. Would mobile applications even allow that?
Is there a simpler way?
also note
I'm using Ionic framework so its a javascript framework
And I'm using nodejs/express as my server
If I were to send a get request to my server from my mobile
application every second(as a watcher) is that bad practice? Or is
that pretty much what firebase's watcher is doing?
This is a bad practice and that is not what it is doing.
I know that when you deploy a web application you can have a backend
inside the directory. Would mobile applications even allow that?
You can't have easily a backend in your mobile application. You can call it but not having one inside your application.
Using Firebase is the good practise.
If you want to create your own server, you can create also a firebase cloud messaging server.
What you are doing is called push notifications. More infos here : https://stackoverflow.com/tags/push-notification/info

How to Communicate Between Web App and C Sharp

I'm attempting to make a Web app that needs to communicate to a program written in C Sharp. But I can't find a good form of communication. What I need is if a user clicks something on the Web app, it will notify the C Sharp program. Also, if an event happens on the C Sharp program, it needs to alert the Web app. Both of these are going to be running on the same machine.
Right now I'm mainly focusing on the C Sharp program just periodically "asking" what the status of the Web app is.
I've tried using POST requests to the Web app and I've had a bit of success with this but I don't know how to essentially store and update a "status" on the Web App. For example, C Sharp program sends a POST/GET request asking for the status, the Web app responds with "nothing has changed" or some sort of status code. I don't know how to keep track of that status.
I've attempted using Web Sockets but I don't think it is going to be possible on the C Sharp side. However, I'm definitely open to suggestions on how this might work.
I've looked into using the ReST architectural style but I'm having a hard time understanding how I would implement it. I'm using mainly AJAX on an apache server and most of the ReST examples I saw used IIS.
One way I've been successful with this is a horrible workaround. I use 3 files to store contents, status of Web app, and status of C Sharp program. But this requires me constantly fetching files, reading them, writing a new one, and uploading it.
Sorry if this question is poorly formed, I'm obviously new to a lot of this and this is my first SO post. Also, I'd include example code but I'm posting this from my tablet so it's not accessible right now.
If they are on the same machine, you can use 'pipes' (Unix), local sockets or file handlers.
These are all types of IO objects both applications can 'listen' to without exposing themselves to the network and without blocking while they are 'waiting' for data..
... But this will limit your scalability.
Another option is to use a Pub/Sub service such as Redis. This is a better option than websockets, because you can have multiple C# apps listening to multiple web apps on a centralized data source.
It uses the same IO concept (using sockets) behind an abstraction layer coupled with a database - it's very effective.
Good luck!
I implemented something similar to this. I needed a desktop application to listen for api calls from the browser. I ultimately decided to implement a "web connector" which can either be created as part of the application OR installed as a service.
Here is a good example: https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx

php mysql background process

i have developing a website that user can chat with other user if they are online . if one user send a message will notify that receiver on their screen , these checking process are happen in background process , i have an option of using
setinterval method and javascript self execution method
but i am looking much faster background process methods which will check every seconds if message or notification arrived .
could any one give suggestion for this.
You can use HTML5 websockets.
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API(JS functions), you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
below is good link to start
http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket
I think the best match for your needs will be http://elephant.io
Elephant.io provides a socket.io client fully written in PHP that should be usable everywhere in your project.
Take a look at the Thruway Project. It's a PHP websocket implementation using the WAMP protocol, which gives you Publish and Subscribe abilities (you can use that for your chat application) as well as RPC.
A good place to start would to take a look at this chat demo (source code) and then use Thruway as the WAMP router.
I'm one of the developers of the thruway project, so if you run into any issues or have any questions, feel free to ask.

Need help working with Phonegap (Cordova actually)

I've being working with Node.js and Angular.js. But now I am moving into mobile (Android) and I would like to solve some doubts before getting started because documentations have being quite confusing. So here are my questions:
How will the mobile app connect to the backend? is it just configuring the config.xml file?
How is the Login system suppose to work when talking from the mobile to the backend?
Does the Login system allow to use the Notification API's? Which variables should I consider?
What limitations would I have when working with Javascript? Like Underscore, D3, Three.js, etc...
If anyone could answer these questions I would really appreciated
1) It depends on what you refer as "back-end". I assume you re referring to some remote server connection. As Cordova is fully based on HTML,CSS and Javascript, you can use some ajax calls to connect to your server(back-end) to get/put data.
2) For my apps, for login system, I use ajax based solution to callback my remote server and validate the login username and password.
3) Yes. You can use the features of Alert, Beep, Vibrate and even push notifications. Refer to the corresponding API docs.
4) Virtually no limitations on what use as long as its javascript and does not create contention with other javascript codes you use together.
It will connect to the backend the same way your web apps do, what transports do you usually use?
Logins will work the same way your web apps do.
There are no limitations on javascript, it will be very similar to developing for the mobile web except you will have more device APIs available to you and you will have the ability to create custom device APIs.

creating a mobile chat app with phonegap - best strategy?

Do I create a timer that does ajax requests every so often to check for new messages? This seems like it would be a huge drain on the phone.
What would be the best way to create a simple PhoneGap instant messaging/chat app?
Is HTTP streaming a tenable strategy for mobile devices? What if the connection is interrupted? Does it work on 3G?
Obviously this is a big question but any help would be much appreciated.
This question is a little high level in scope and a lot would go into a "total answer". It depends on what you're using but have you considered using node.js and websockets? I know there are other ways to do server side websockets but that is a popular one.
If you're not familiar with web sockets, it's a new HTML5 technology and the mobile platforms seem to be keeping right along with HTML5 technologies so this may be a viable solution for you if you're still looking for something.
The thing with web sockets is that you don't poll for new data as what you have speculated in your question. It maintains an open connection between the server and the client. That way, the server only needs to push new data to the client when it arrives and vice versa when you want to push data to the server. You use a lot less data and processing since you don't periodically check for data. Also it's more of a "realtime" experience since messages are propagated after the server receives them.
Doing a general google search for websockets and chat yield all kinds of tutorials available.
Hope that helps.

Categories