How to create cleint/server applications - ajax

I have been trying to make multiplayer applications on a website for a while. I wanted to start with a basic chat system. I made one but it is really slow. On the HTML page it send the message through AJAX to a PHP application which saves it to a text file. Then back on the HTML file, it is constantly checking the text file every 3 seconds. This is very slow and unreliable. So i looked up better ways of doing this. I found Node.js and used it along with Socket.io and express to create a faster chat application. But it only works on local host and i have no idea how to implement it on a website. So I kept looking and discovered WebSockets. Which are so confusing and seem to have very little support. I am confused how websites out there have applications that can be real time with so few options. How is this done? Am i missing a way of doing this? If you can help me that would be great.

Socket.io is using websockets under the hood already. Using raw websockets isn't necessary for your chat app.
You're on the right track using socket.io and node.js server-side.
Building a multi-player game in the browser will be a very difficult task for a beginner. But that's why it's good to learn! I suggest using a library for graphics (a quick google gave me this: http://www.pixijs.com/ ).
The over all architecture should be something like:
Users go to your server and receive a web page (.html) which contains the javascript and a canvas they need to play the game. This is the "client-side" because it's all running on each users' web browser on their computer.
The web page runs the javascript which talks to the node.js server using socket.io. This is the "server-side". The job of the server is to coordinate player data (who is who, where are they in the game, who's doing what etc) and keep track of the game state. Basically, this is where the game actually is, kind of like having the Monopoly board on the server, while the client-side is really just responsible for showing the board to the players (drawing it on the HTML5 canvas) and sending player input to the server.
Tutorials:
USE GOOGLE. Try just literally searching for "javascript game tutorial". Try every tutorial that comes up. If something is taking a while to get up and running then ditch it and move to another one.
Do simple little things at first 'till you start to grok what the overall process is like. For example: https://developer.mozilla.org/en-US/docs/Games/Workflows/2D_Breakout_game_pure_JavaScript
Remember playing ultra-simple games like Pong? Try writing games like this first. Your chat system is a great start, by the way, because it covers the basics of how to get a server up and running, how to get a page, how to send data around.
As for getting something up and running on a server where others can connect to it... Check this out: have your buddies come to your house and bring their laptops, start your node.js chat server, tell them what your IP address is, and have them go to "http://YOURIPADDRESS:8000" in their browser-- they'll connect to your node.js server!
Getting it running on a hosting provider is a little more involved and probably not worth the trouble at this stage. You'll learn more about this later just by keeping on the way you're going.

Socket.IO does not only work on localhost. You will need to get a server to run your application on. I highly recommend not worrying about this piece of the puzzle just yet, as it is somewhat complicated if you are brand new at this. Come back to this part when you are ready.
As for the game development, I recommend using Phaser. It has everything you need to get started and great documentation.
http://phaser.io

Related

Implement multiplayer poker game with PHP and HTML5?

I want to create multiplayer game with PHP.
I created full OOP system for that - classes that management the whole game.
I also stated to write the HTML+Canvas code for the game - and now i have a problem.
For real HTML5 multiplayer game i need to use Node.js + Socket.io technology.
But i don't want to leave all my PHP code and start implement all again with Node.
So i tried to use AJAX Polling so players can update the server every time they do something in the game - but the problem is - if any player close the browser or hack the javascript game timer - the server can't know about because PHP code is execute one time.
I thought about create a cron-job that run every second and check if player is still active, but cron-job is limited to execute every one minute (and also i don't think it's correct method).
so what can i do? i need to leave all my work in PHP and start learning and implement all again with Node.js?
There is no other option to make the server run even if PHP code has already execute?
Just a personal opinion, I am a big fan of PHP, I love it, however I do recognize when PHP is not the answer for a particular problem. PHP was not designed for real time applications, sure there are a lot of solutions for that: Ape or Ratchet but at the end I feel like I am consuming more CPU cycles than I really need. I would stick to Nodejs and socket.io since they are meant to be used by real time applications. Again, this is just a personal opinion.
PHP it's really not what you'r looking for...
you must use better technology like NodeJS & Socket IO
You can implement long polling in PHP with set_timeout(0), and then perform one AJAX with endless loop, but it's really lame
I know this question is bit old - but I just complete development a texas hold'em game with NodeJS and MongoDB and it's really incredible!
PHP is out of the question when you have such those technologies!

Phalcon php vs node.js

We are going to develop rest server for our application (and all logic is on client javascript).
So we thought to use Phalcon php, but we also need to create realtime chat system, which is much more easy to do using node.js. This made us think about using node.js instead of phalcon
Unfortunatly, we are not good expirienced in node.js, we love phalcon for its performance and internal beauty.
The quiestion is, did anybody compare phalcon and node.js performance? May be it's better to use node.js only for long polling chat requests, but i dont like when project is connected with so different tools.
You are trying to compare two different things IMO.
node.js has a lot of power and flexibility but so does Phalcon. If you want to create a chat application with Phalcon, then you will need to implement some sort of polling mechanism in your browser that would refresh the chat window every X seconds. Getting/Inserting the data from the database will be Phalcon's job. Javascript will be used to do the polling i.e. refresh the chat page every X seconds.
The problem with this approach is that you might be hitting your web server every X seconds from every client that has the chat application open - and thus refreshing the chat contents, even when there are no messages. This can become very intensive very quickly.
node.js has the ability to send messages to the subscribed clients instantly. Web sockets can do the same thing I believe.
Check this video out, which will give you an idea of how this can be achieved easily:
https://www.youtube.com/watch?v=lW1vsKMUaKg
The idea is to use technologies that will not burden your hardware, rather collaborate with it. Having a "subscription" notification system (such as sockets or node.js) reduces the load on your application since only the subscribed clients receive the new messages and no full refresh is needed from the chat clients.
Phalcon is great for the back end with its speed and it can be used to construct the message which in turn will be passed to the transport layer and sent to the client. Depending on how you want to implement this, there are plenty of options around and you can easily mix and match technologies :)
as #Nikolaos Dimopoulos said, you're trying to compare two different things.
But here is my advice, while you're experienced with PhalconPHP framework, and you want to benefit from Phalcon speed and performance, you can implement the web app in Phalcon FW, and the chatting system in Node.JS as a service.
If your web application "The Phalcon app" needs to push messages from the backend, you can use http://elephant.io/ library for that, I have done this before with Yii framework and Node, and it's working perfectly.
My advice is to use what you already know, experimenting with nodejs just for the chat application.
Mainly because you said you do not have experience with it, so, because the chat app is something a lot of people made you'll find plenty of examples.
By doing so you will learn a lot from node and might even think about migrating from Phalcon if it suits your needs, using the features offered by expressjs for example.
I would not choose one over the other based on performance.

Making a chat app using AJAX, Servlet and JSP on GAE

I'm a CS student trying to do some side projects during this summer. One of my aims to is create a chat app which will be ultimately hosted on GAE. I am new to web development so I'm trying to shoot around in the dark hoping to hit the target but I guess it will be a major waste of my time. The rationale for using servlets and JSP is that GAE requires Java for the backend. I hope to use AJAX to do the front-end.
However it is hard for me to put all the technologies together to make it work. I am having trouble with the design. I don't need any codes, but rather help with the design patterns.
I am confused with how GAE works. Since GAE requires Java/PHP/Python etc, is it possible to deploy the client coded in AJAX using GAE? Do I require two GAEs, one for the client and one for the server which is coded in Java?
I am also quite lost with how to connect the AJAX technologies with the Servlet & JSP technologies. I'd appreciate it very much if you guys can provide a step by step instruction on the design pattern. Links to online tutorials will be very much appreciated. My style is to learn as I go.
Ultimately, my aim is to get an chat app (very simple one where all users can see each other messages) up and running on GAE to get a feel of the whole web development process (code, run, deploy).
Just a side note, I don't know any PHP/MySQL (but will learn later if I get the whole web dev thingy down to include database features).
Thank you all.
There's a LOT of stuff available out there to read if you just search for Google App Engine. Start with the documentation and work through the tutorials. It's not a waste of your time to learn, since you don't already understand it.
Google App Engine is essentially a distributed web server + database. AJAX on App Engine is no different from AJAX anywhere else - the server serves HTML+Javascript which runs on a web browser, and communicates back to the server.

opc server written in java makes my java UI stop working

I have created a user interface in java which uses card layout. It basically shows a lot of readings in it which it receives through our company's local network. Now I had to write an OPC server in java so that all the readings my ui receives is stored onto that server. I successfully wrote it. My UI works awesome for almost a day but the next day when i come to office, I find my UI frozen, as in there is no more communication taking place. The UI stops receiving or transmitting numbers. When i run the UI without OPC, it goes on and on forever but its when i add OPC to it, that it freezes/ I have tried all sorts of stuff but I still have not been able to solve it. ANd yes, I have used Jnet Pcap to capture the packets in the UI. There is a huge amount of code in it so if anyone requires any specific code to look at, let me know.
This is really hard to follow. What is the exact problem that you are facing is still unclear.
The openSCADA website that has the free OPC implementation should be your best bet...
http://openscada.org/projects/utgard/
More over you can try the JEasyOPC client which is also free incipiently.

Is there a super-high-load (Ajax) chat script out there?

For a pet project, I have been looking for a web chat script capable of running potentially tens of thousands of users simultaneously. I don't want to use any kind of applet or browser extension, so on the client side, it should be simple Ajax. On the server side I'm pretty much open to anything.
I'm not looking for bells and whistles, a simple text-only chat is more than enough, as long as it supports a number of 'channels' or 'rooms' simultaneously, and a very large number of users.
When I first started researching the chat scripts out there, it seemed like the only viable option was to run an IRC server and just build a web interface on top of that. I know I could get good performance and stability with that setup, but could I get better performance by using something else?
Any ideas?
You might want to check cometd
I believe there are some chat scripts already using cometd.
I have no idea regarding stability tho.
You can have a look at Jabbify.
Not sure about the rooms and channels part, but it is built on the AJAX and MVC model.
I am going with Twitch.me, which is based on node.js

Resources