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!
Related
I'm creating PHP MySQL real time chat app. A friend told me It is very bad to use PHP for real time apps & that would kill server CPU.
I know that PHP isn't the proper choice but i wanna get some advices to make the performance better with using PHP for real time chat apps.
I also wanna know why some developers prefer using PHP7 over NodeJS/Socket.IO/GO/..etc ?
Thanks,,
I would prefer Nodejs + Socket for realtime chat application because of one reason that it will be completely Javascript from Browser/Mobile to server and if NoSql is database than upto database as well.
But your point of PHP7 vs Nodejs/Socket left me with a question and to get the answer I came across this blog. This might help you as well. Its too long to explain it here.
PHP7 vs Nodejs
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
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.
I am working on a Project where I will try to compare different ways of creating a live ticker (news-ticker or sport-ticker whatever). I would also like to make benchmark tests, like how many connected users or how fast the data will come.
I have googled and found that usually tickers are build in PHP as the server and the client has an ajax call for every 30 seconds which will return the new Data.
Another way of doing this is with websockets (socket.io) and node.js!
With those two, I have worked and experimentet!
But are the other ways of building tickers?
Obviously you may replace any server-side language with php but are the other ways that the server sends data to the client?
Maybe with Flash ? or Ruby on Rails?
It would help if somebody could point out common ways of tickers and also modern ways or fast ways. I will then choose two of them and compare them!
I would really appreciate if someone knows good articles or links about this topic.
Thanks
I think it really depends on your requirements (update rate, etc.). I means if you update the news only a couple times a day, you will have some overhead if you maintain a Websocket open for nothing. All server technologies will do the same thing if you are requesting the data every 30 seconde or something like that, it is a typical ajax call. Did you considered using something that is already build like Jquery News Ticket
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