Two player chess game using Firebase - firebase-realtime-database

Edit: The answer is to use firebase realtime database.
I wrote a library for the next person.
https://github.com/flipflopapp/turnbased-games-with-firebase
-- Question --
I am implementing two player chess game (www.halfchess.com) and am considering using firebase messaging (instead of using sockets to create rooms and two player matches). The game would involve sending 60-100 chess moves as messages between two devices in two to three minutes (that can be android or iOS). My nodejs server would have code that enables device to device messaging (receiving from one player and sending to other).
I cannot use Google Game Services because I don't have google login implemented in my app (I only plan to keep facebook login). The advantages of using firebase (compared to sockets) is that I will have to write much lesser code (reconnections, etc) and it would take care of scalability issues.
My questions are :-
(1) Will there be problems when the users playing against each other are on two iOS devices (instead of android's)? (such as higher latency)
(2) If a user is changing location physically and a message that contains a chess move is undelivered, when will it be retried?
(3) For a fast game of chess, will the latency be manageable? This is like 8-10 times the speed of normal chatting.
While I read more on the topic, perhaps someone who has already experimented can comment.

Firebase Cloud Messaging is not meant for kind of usage, and in addition to a non guaranteed delivery time (some researches from 2013 - 2014 shows more than 1 seconds per message on avarage), FCM will probably imply throttling in such a use case.
See also this SO post

I'm sure the answers above will work but, I was having a tough time getting them to function. This is what eventually worked for me and my firebase chat app!
Hopefully, this will help some folks out there.
I was able to add a chess game to my firebase chat app and, all I used was an iframe! However, it didn't work the first time because, all I did was add the iframe coding to my app.
This is how I got my iframe to work in a firebase app...
First, change directory (cd) into your chat app's "public" folder (where you would typically run the "firebase deploy" command) and, add your iframe to the "index.html" document located there. Use this address for your iframe's source URL (src)...
src="chess/index.html"
It wont work right if you do not include the "index.html" page name!
Next, I created a new folder named "chess" in the same public directory and I added the chess game's "index.html" doc and dependanciess to it (js,css,images...etc).
And last, but not lease, open a terminal in the same "public" folder and run "firebase deploy" to upload the whole thing to your firebase account and console.
Done!
I'm pretty sure that including your chess app docs inside of your firebase app is what made the iframe finally work. I also wrapped the iframe with a couple of 'div' tags but, I'm not sure if that made any difference.
Please, feel free to come and take a look but, you'll have to sign in with Google to gain entry!
After that, just right-click anywhere on the page and select "view source" to see the code. Cheers!
https://friendly-chat-b2d6a.firebaseapp.com/

Instead of having the other player send a message to the client, why not just have the client display a message based on whats happening in the game? It seems like an easier solution for you, as the only thing that needs to be sent is the actual move, and you can piggyback off of that if you need to.

I realized I should be looking at Firebase Realtime database (and not messaging).
Useful links:
https://firebase.googleblog.com/2016/07/have-you-met-realtime-database.html
Is firebase realtime json database suitable for data broadcasting?
https://firebase.google.com/pricing/
https://groups.google.com/forum/#!topic/firebase-talk/n_B1nrgp580
(according to this talk the latencies can be < 200ms most of the times)
https://twitter.com/jonikorpi/status/733560092780462080

Related

Matching time on iPhones worldwide - synchronizing playing video?

Two Quick questions:
Are the seconds value of the clock/time on all iPhones around the world the same? If not, are they close at least & how close?
Do the seconds value of the clock/time on all iPhones around the world change/increment at the exact same time?
Upon request, I'm editing this post and adding the purpose for asking such questions:
I'm trying to make a corporate app that can play a video on multiple iPhones around the world at the exact same time (or as close as possible, ideally the exact same moment). Could you please guide me on how to do this?
Much thanks in advance!
To answer your actual question per se,
Are the seconds value of the clock/time on all iPhones around the world the same?
The fact is, yes, 99.9999% of iPhone users simply use the "get time from a server" system which is of course built in to any phone now.
(Indeed, this simply applies to any Windows, Mac, Android, iOS, etc etc device.)
Yes, they are all "about the same".
You could rely on it being within a second or two, probably even closer.
You cannot rely on it being closer than that.
It seems that you
I'm trying to make a corporate app that can play a video on multiple iPhones around the world at the exact same time
Synchronization in general and streaming video (is it streaming?) is a well-explored (and rather technical) branch of software engineering. (For example a massive amount of game engineering, which is a huge field, relates to inside-the-frame synchronization.)
This is not something you can learn how to do in five minutes, and it requires a stack of device-cloud stuff. Go ahead and ask new questions about this, or just google to get started! Good luck.
Regarding using push notifications.
With the push notification you would send a time. You would not send a "command to play it".
So say right now it is (example) 09:13:28. You would pick a time in the future by a couple minutes. So let's say 09:14:30.
Then using a push notification you would send that information "09:14:30" (and a video file name) to everyone connected. (You'd be sending a "command" as it were, to play video X at 09:14:30.)
Then every device would in fact play the video at 09:14:30 (simply using the local clock, as asked in your question).
Be aware that sending push notifications is extremely sloppy and slow. It can take any amount of time from 5 seconds to a minute, AND quite often there are delays beyond that (ie ten minutes or the like).
I personally would not even bother starting to experiment with push notifications, for the project you describe.
These days, making apps is entirely about using device-cloud services, such as Firebase. Everything is about "OCC" - occasionally connected computing.
(So, you can't get a job "making apps" anymore - i.e. if you know how to move buttons around on an iPhone screen. You get a job because you can make a total, live, device-cloud system - indeed such as you are making.)
Indeed your example project is the perfect such "demo" project for learning about how to do modern apps.
Simply use Firebase to sync everything up.
You'll essentially put a piece of information on Firebase ("play video X at 09:14:30") and that information will be communicated fairly quickly/reliably to everyone connected.
For the particular task you describe, I personally would use PubNub which is faster than Firebase and basically made for game-like problems precisely like you describe.
http://pubnub.com
If you truly needed performance/reliability better than pubnub, you are really talking major engineering. So, the (buildings of) engineers who make live games at Nintendo, Warcraft etc, would tackle such an issue as "being even faster than PubNub".
So, the answer in brief!
The very short answer then to your question posed is:
Learn to use the various device-cloud services, which are at the heart of all apps today. (Knowing how to make "an Android or iOS app", as such, is of no consequence today.) For your particular problem, you'll want to use PubNub specifically, as it is built for precisely realtime problems such as this. (Firebase more leans towards "OCC" type data problems.)
Really that's it.

WoW Lua - Get Data from URL (Vanilla)

In a World of Warcraft Vanilla Lua Addon Development, how can I issue an HTTP call to receive data back? If not, how can I get data from a web source into the game while playing?
I have a feeling the answer is tragically short, but would like the question asked and answered on Stack Overflow. My research came up lacking, and I recall doing some LUA in ~2007 and was disappointed.
Well, tragically short is an understatement. You simply can't. There was never any APIs that interacted directly with connections, let alone create any, let alone to arbitrary URLs.
Most of them just broadcast game events that occur from the game's connection, and the closest thing you can get to a "data stream" is add-on chat channels. But since bots violate the ToS, you wouldn't be able to make an account that responds to your addon's inquiries.
The closest thing you can get is building an "asynchronous mesh network", but that's only good if your addon has a considerable user base, and it's not guaranteed you'll get information timely.
The general idea is that your addon will have a public key (as in encryption), and you (only you) will detain a private key. Your addon emits a message to any connected peers, which store it on cross-realm SavedVariables, and you hope that someone will have characters on more than one realm. Upon login, the client addon will broadcast its latest packet (still encrypted) to that realm's addon channel, and hopefully within a week or so you might get the updated information across all clients.
A disadvantage is that you'll only get "push" notifications, the client won't be able to send any data back to you*.
That, or you could release a patch for the addon on Curse :P
BUT WAIT!
You mention vanilla, so I can presume you're developing this for a private server. Private servers often have one or a very small amount of realms, making the above mesh network much simpler. Instead of a mesh, just have encryption and manually login&broadcast on each realm every time you want to update the information retrieved.
Plus, you might even be able to contact the server devs to allow you an API that sends messages to the appropriate ingame addon channel (you'd have to ask).
Of course, if you pretend to make your addon server-agnostic, instead of tailored to a specific server, you're back to square one.
 * Unless you are really dedicated to make that happen, because it's a ton of work.
There is no web API in vanilla WoW. There is a web browser widget in the game currently though, albeit very limited in usage.
If you have access to the server software code, you may be able to hook listening on specific game channels for user messages in a defined format, and let the server respond in a way for the addon to parse it.

Linking online databases to a project for iOS

I'm working on an iOS app for having a list of top 10 of everything.
So the problem I'm facing is how do I update the list every week ?
I thought of linking it to external links but then I was unable to format the webpage according to the interface of my app.
Lastly I want the app not only to display the top 10 list but also to be able to interact. For example if the user enters into top 10 songs then he should be able to play the tracks as well ! So for this I guess I'll be needing a database which I could update weekly. I don't know how to go about this. Please help !
You need a remote server, running with an application that handles the list generation/updates, and you will probably need also to manage a remote database, if you expect that the number of item will be reasonably large.
Your ios application will have to interact with your remote server using an API that you will have to define as well.
I'm sorry to be that generic, but the hard truth is that there is no quick or easy way to do it: you will need to learn and possibly develop every part of your system and then integrate them.
You have nearly an infinite number of options in terms of platforms/languages/databases to choose from, for the backend part.
Over the last few years, there have been services that should make handling the remote backend easier (Stackmob, cloudbase...) but it really depends on much control you want to have on the "lists" you want to manage.

Watch video in the time they are uploaded

It is possible to implement a feature that allows users to watch videos as they are uploaded to server by others. Is html 5 suitable for this task? But flash? Are there any read to go solutions, don't want to reinvent the wheel. The application will be hosted on a dedicated server.
Thanks.
Of course it is possible, the data is there isnt it?
However it will be very hard to implement.
Also I am not so into python and I am not aware of a library or service suiting your requirements, but I can cover the basics of video streaming.
I assume you are talking about video files that are uploaded and not streams. Because, for that, there are obviously thousands of solutions out there...
In the most simple case the video being uploaded is already ready to be served to your clients and has a so called "faststart atom". They are container format specific and there are sometimes a bunch of them. The most common is the moov-atom. It contains a lot of data and is very complex, however in our use case, in a nutshell, it holds the data that enables the client to begin playing the video right away using the data available from the beginning.
You need that if you have progressive download videos (youtube...), meaning where a file is served from a Webserver. You obviously have not downloaded the full file and the player already can start playing.
If the fastastart atom was not present, that would not be possible.
Sometimes it is, but the player for example cannot display a progress bar, because it doesnt know how long the file is.
Having that covered the file could be uploaded. You will need an upload solution that writes the data directly to a buffer or a file. (file will be easier...).
This is almost always the case, for example PHP creates a file in the tmp_dir. You can also specify it if you want to find the video while its being uploaded.
Well, now you can start reading that file byte by byte and print that data to a connection to another client. Just be sure not to go ahead of what has already been recieved and written. You would probaby initiate your upload with a metadata set in memory that holds the current recieved byte position and location of the file.
Anyone who requests the file after the uploaded has started can just recieve the entire file, or if the upload is not yet finished, get it from your application.
You will have to throttle the data delivery or pause it when the data becomes short. This will appear to the client almost as a "slow connection". However you will have to echo some data from time to time to prevent the connection from closing. But if your upload doesnt stall, and why shoud it?, that shouldnt be a problem.
Now if you want to have someting like on the fly transcoding of various input formats into your desired output format, things get interesting.
AFAIK ffmpeg has neat apis which lets you directly deal with datasterams.
Also handbrake is a very good tool, however you would need to take the long road using external executeables.
I am not really aware of your requirements, however if your clients are already tuned in, for example on a red 5 streaming server, feeding data into a stream should also work fine.
Yes, take a look at Qik, http://qik.com/
"Instant Video Sharing ... Videos can be viewed live (right as they are being recorded) or anytime later."
Qik provides developer APIs, including ones like these:
qik.stream.subscribe_public_recent -- Subscribe to the videos (live and recorded)
qik.user.following -- Provides the list of people the user is following
qik.stream.public_info -- Get public information for a specific video
It is most certainly to do this, but it won't be trivial. And no, I don't think that you will find an "out of the box" solution that will require little effort on your behalf.
You say you want to let:
users watch videos as they are uploaded to server by others
Well, this could be interpreted two different ways:
Do you mean that you don't want a user to have to refresh the page before seeing new videos that other users have just finished uploading?
Or do you mean that you want one user to be able to watch a partially uploaded video (aka another user is still in the process of uploading it and right now the server only contains a partial upload of the video)?
Implementing #1 wouldn't be hard at all whatsoever. You would just need an AJAX script to check for newly uploaded videos, and those videos could then be served to the user in whatever way you choose. HTML5 vs. Flash isn't really a consideration here.
The second scenario, on the other hand, would require quite a bit of effort. I am guessing that HTML5 might not be mature enough to handle this type of situation. If you are not looking
to reinvent the wheel and don't have a lot of time to dedicate to this feature than I would say that you would be out of luck. You may be able to use ffmpeg to parse partial video files and feed them to a Flash player, but I would think of this as a large task.

How to display live data in iphone app?

I am building an iphone app for my local high school football team. What is the best way to show live data for the scores of games without using a sql db and having to updated the entire app after every game.
there are a few ways of doing it, one would be to create an rss feed on a remote server, and parse that, and if you wanted realtime updates, you could use push notifications.
If you plan on creating the app to only show what the end result of each game was you can use NSXMLParser as #MCannon suggests. If you plan on having the app update in REAL TIME, as in. "The ball is on the 45 Yard line, 3rd and 3". Then you can stick with NSXMLParser but create a NSTimer to update the information every 30 seconds or so.
That would also mean you would need to have the RSS feed updated in real time as well.
The only way to do what you're asking is to have the data stored on the internet in some fashion, then build into your app a routine for consulting the remote data source (on the internet) and update its internal storage accordingly.
It can be done with no DB but the data must exist and be accessible over the internet. As others have suggested, an RSS feed would be a great way to pipe in the data.

Resources