PHP Workers & Working with Modern Javascript Frameworks

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Fireal
Posts: 11
Joined: Sun May 21, 2017 5:51 pm

PHP Workers & Working with Modern Javascript Frameworks

Post by Fireal »

Hey guys, I'm back with another question for you fine gentlemen.

1.) What in the blue hell is a PHP Worker?
Is it just like a way to use SESSIONS and store persistent data for non-web apps?

I'm working with some javascript based frameworks and most of them won't accept php without using a worker or ajax call. I really enjoy PHP and have great workflow creating my game logic within it but it seems like if I want to stay relevant with my games I have to push towards these frameworks.

2.) What is the best approach to load php files using javascript/ajax in an efficient way?
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: PHP Workers & Working with Modern Javascript Frameworks

Post by a_bertrand »

1) https://kinsta.com/knowledgebase/php-workers/ => think about how many processes can handle some work at the same time. I would suspect that for a game at the beginning 1 worker will do the trick.

I'm totally biased here, but I would say generally => stay current? Then stay away from PHP.

2) That's partially due to the 2nd part of my first answer => there is no real good solution to do web sockets in PHP. And that would be the most efficient way to have a 2 way communication with your server.

Another option is the so called "long polling" like: https://github.com/panique/php-long-polling
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP Workers & Working with Modern Javascript Frameworks

Post by Jackolantern »

To the specific question, a "worker" typically means some type of thread. If you aren't familiar with threads, they are basically an independent executing block of code. One of the easier examples I know of that demonstrates the use of a thread is the way GUIs are programmed for desktop applications. If you think about the way programs typically work, when you go to read a file or perform any other type of input or output (I/O), the program actually hangs-up and waits until the file is opened/written or whatever you were doing. If you are debugging and stepping through a program line-by-line you will notice lines where you open a file or open a database will take longer to finish. Programs are often doing this kind of input/output all the time. And if you are writing a desktop application, your program is also running the GUI. If you wrote your entire GUI desktop application in one piece, the entire UI would freeze whenever you were opening a file, connecting to the Internet, etc. This is because once you open a file, a socket to connect to the Internet or do some other type of I/O, your whole application would be waiting on that operation to finish before it could get back to painting the GUI. This could be a few milliseconds in the case of opening a file or it could be minutes if waiting for a response from the Internet! One way of fixing this problem is by having the GUI run on its own thread. In fact threads were really the only solution to this problem. There are several more now but they are outside the scope of this discussion.

Workers are basically lightweight threads. You are not often going to need either PHP or Javascript threads. You can make AJAX calls just fine without them. In fact, PHP workers did not even exist back when I used to work with PHP and thus I am not really sure what their main usecase is unless it is for long-running calculations or something like that. Their main use in Javascript is for making offline apps since Web Workers (the JS workers) have a special feature that allows them to cache or intercept HTTP calls.

What Alan mentioned is true, though. web sockets do not work great with PHP. However you may not need web workers depending on what kind of game you making. Making steady AJAX calls can come pretty close to replacing web sockets and will keep your application's server-side code much more simple. The main thing you can't do (easily) without web sockets is initiating calls from the server to a client. Clients can call the server easily with AJAX, but the server cannot easily call a client with AJAX. For fast-paced, real-time games (think games like World of Warcraft, Fortnite, or an online multiplayer game of Pac Man) you will need two-way communication. But tons of games can be with just AJAX. Particularly turn-based games.

Hope this helps. If you have any more questions let me know! :ugeek:
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”