javascript + mysql live changes - javascript

I read several tutorials, but I couldn't find a working example for my case.
I want to have a live-refresh (side-wise), whenever there is a change on the database (lets say I'd change values via phpmyadmin).
From what I understood, socket.io should be one solution. Also, I found this tutorial here, but it seems outdated, so it doesn't work anymore (from what I can tell).
Are there any alternatives or working examples? It can be pretty basic, but using setInterval with a param in range of 100 - 500 seems a bit to heavy, right? I can't wait for more than 1 sec.
I'm not sure, if there are any listeners or service, which can "push" a change to a JS-site, so I wouldn't rely on the JavaScript timeout-function.
Also, from the docs (and examples) on socket.io, I found ways to run the server on localhost, but not on the server. Lets say, I have this:
var.app = require('http').createServer(handler),
...
app.listen(8000);
it seems to listen to localhost:8000. Can I just replace this param to a live-url?

You could use WebSockets, it's already widely supported, but you still need to have a WebSockets server running on your machine, you still would NOT be able to actually send changes that you have made in phpmyadmin, because PHP does not know if you made any changes there.
The solution would be really complicated probably, because you would have to combine options, most of the stuff you could probably solve with WebSockets, so you would just send notifications from PHP to JavaScript, but apart from that, you would also have to check the database for changes, if you really wanted to monitor changes by you in phpmyadmin ( which I strongly advise against, it is easier, to create functions that modify the database in a way that you want, then to check the database every some ms ).
Your structure would probably look something like:
JavaScript -> connects to -> PHP WebSockets server
and on every change you submit the change to JavaScript.
Sorry, I cannot help you on setting the WebSockets server, but I know that Symfony2 has a package for it, you could go download and reverse-engineer it.
Here's the MDN page: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

Related

How to detect Minecraft Singleplayer LAN port

Some Backstory
So I really need help with this project I am making that is suppose to try to find an open port from my Minecraft SinglePlayer LAN. As making something like that is easier then creating a server. To help with my project, I use this API https://mcapi.us/, which helps getting the status.
Firstly, I did try to use a for loop to loop through a huge range but with registering it through the API, it overloads my computer CPU. Secondly, I tried finding some help online and found about web workers and saw I could divide up work. It was about perfect but then seen that I cannot use the API (The most important part) on it as the elements that is in the API, web-workers cannot use.
The Problem:
So the problem is that I cannot properly range through a huge number like 10000 to 65535 as just running that through the API method overloads my CPU. I have also tried using PHP as well but no luck as it takes too long to read through all the numbers. I am probably going to have to use PHP though as it seems to be more powerful. But I need some way to get the Minecraft Singleplayer LAN port and I can't find any way to do that at all.
Reason:
The reason I need some way to access it is so I can make a web application to post the port the Minecraft Singleplayer LAN is running on and if offline will say another message. This way I can just send the web url to a friend and they can continuously just copy and paste the address and port for easy access so I don't have to keep sharing the port as that is non-efficient. One more thing as well, I am using Minecraft version that is before 1.13, not at 1.13 or after.
Question Update:
I have in fact made this question more clear to what I originally meant as what I said people was unclear so I updated this to be more clear. Plus Stack Overflow is kinda forcing me to make it more clear. So here, I hope this is clear for everyone.

How can I have a page showing reservations update when a customer adds a reservation from another computer (using Rails)?

I would like to have a page where a restaurant can log in and see all of their current reservations/take-out orders, and I want this page to automatically update when someone (from another computer) makes a reservation or places an order. The idea is that the restaurant would leave this page open at all times to show their current status. What is the best way to do this? Can it be done without refreshing the page?
I wasn't even sure how to refer to a setup like this, so I wasn't really able to find much using Google. Is there a word for this type of setup?
I am using rails, and I am considering using AngularJS for the front end. Any suggestions?
There are two approaches to solving this.
The first, oldest, simplest is that your webpage contains some javascript that will poll the server at regular intervals (e.g. every 10-30 seconds), to check if something has changed and then add the changed data (e.g. reload a partial).
The second approach is a bit cleaner, and it allows the server to push the changed data to the connected clients, only when it is changed.
There are a few available approaches/libraries for this:
use websockets
use pusher
use juggernaut The author of juggernaut had deprecated it, in favor of using HTLM5 SSE (server sent events). Read more.
The advantage of using polling is that it is easy, works on every browser, but you have to write more code yourself, you will put some kind of load on your server, even if data has not changed (although the load is minimal).
The push-technologies are newer, work very clean, less code is needed. But some work only in newer browser (most of the times not really an issue), and some require extra support/setting up on your server-side.
On that note: pusher is really easy to get started with, and if your load is limited, it is free.
There are still a lot of others, but this should get you started in the right direction.

Is it possible to retrieve data from parse.com using objective-c and show it in website?

I have an iOS app in which I use parse.com as backend service. Now, I hired someone to do a website interface using HTML and CSS. I want to share the same data between iOS app and website, I know parse.com offers me a few ways to do this, including creating a javaScriptapplication. The problem is, my programmer doesn't have any experience in JavaScript, nor do I.
My question is: Is it possible to use what I have (objective-c, xcode) as far as retrieving data from parse.com and showing on website? Even if I need to code something new, is it possible to use objective-c together with HTML and CSS?
Thanks.
Parse has several APIs, one of which is REST. Your web developer should use the REST API to get data from Parse
https://www.parse.com/docs/rest
If there is will there is way, but you'll be making something really specific to your use and will be non standard and will be immediately hard to maintain, I recommend that you hire another developer and do things properly using the technologies given to you by parse !. if the cost will be high now I can promise you it'll be much higher if you went the path you're going to now.
So my answer is:
Yes, everything is possible and no, don't do it ! :)
Edit: Added an example to a possible way to do it to actually answer OP's question.
Example case:
1-Create a simple Mac Application in Xcode that fetches data exactly like you do it on iOS, and store the needed data into a database of your choice on your server
2-You now have access to the data you needed from parse, but on a local mirror. you will need some tool to fetch that data though, I recommend a simple PHP script.
Note that this will require an OSX server to always be running to fetch that data, you'll also need of find a way to fetch data on demand when a user needs it Vs. polling at specified intervals, this will hardly scale and will be costly as I said.

Can I create a listener in JS instead of repeatedly requesting a URL?

I'm working on a tool that will require 'listening' for a response from the server.
Currently I've got the page using JQuery to request a URL and respond based on it's output.
I do that every couple seconds.
However, as there will likely be hundreds of people using the tool all at the same time, that could be a pretty big server load.
Is there a way I can create a 'listener' that will notify the loaded pages when a change happens instead of constantly querying the server?
I haven't really been able to find much on Google (probably not searching for the correct thing) so hopefully someone here will know exactly what I'm talking about.
Thanks in advance for your quick responses!
You are looking for technologies named Comet or server push. There are several different implementations of this problem, typically involving long-running, but idle HTTP connections. Check out Atmosphere (in Java) or various other libraries.
Also make sure to have a look at web sockets (new HTML5 technology).
See also
COMET javascript library

Do you have any idea how Google Docs Javascript do the interval data autorefresh?

Alright, Here it goes:
I'm currently implementing a software which autorefresh/autopull/autoreload the data to keep the screen live by using AJAX.
This is actually working, but I know I´ve used the simplest approach which is:
SetInterval (javascript)
Call the Refresh Method over and over each n seconds.
Read the Json Data, rebuild the HTML and update it.
This can also be done by just calling a SetTimeOut (javascript) and the end of the AJAX request.
In the refresh method I internally check that it´s not being called simultaneously, etc.
However... this is the simplest approach, it works but, in slow computers, firefox and ie, I can see this activity sometimes freezes the browser, and I know this might not be necessary because of the AJAX call, but how "intensive" is the javascript operation overall... but, after running a profiler, Overall javascript (using jquery by the way) seem to be fine. Also if I disable the autorefresh, the browser wont freeze by short seconds in slow computers.
I decided to investigate how several of the majors AJAX applications works out there.
Facebook for instance.. they do a request all the time, every N seconds, interpret the JSON and update the screen, but, google docs... I can seem to find any request.. This is maybe because: they are just telling the javascript debugger engine that they do not want their request to be logged??, or, are they using another approach to the refresh dilemma?
I read in another answer here at stackoverflow, that Google Docs keeps an open connection..
Can this be the answer? http://ajaxpatterns.org/HTTP_Streaming
What do you guys know about this?
Just as a side note, the application I´m developing is meant to be accessed by thousands of users at a time, and I know the JavaScript refresh routine only tells a little part of the history, but the Server Side Application and the database is currently supporting such a load according to the stress tests I did by using several thousands of virtualized stations. I just want to know what you think about the client browser problem specifically.
Regards and
If you are still reading this..
Thanks you for your time.
I suspect they're using WebSockets. Browser support is flaky, so your mileage may vary with this approach.
You may also want to look at APE (ajax push engine), which is a decent implementation of long polling with a client/server architecture.
You can read up on Long Polling. But then you'll have to handle dropped connections etc.

Categories

Resources