Looking for MySQL performance advice for database-heavy app - php

I'm creating a messaging app using jQuery, PHP and MySQL. Every time a user enters a message, I store it in a MySQL table. On the receiving users end, I basically just added a Javascript timer to check the database every X number of seconds for new messages.
The system works well but is this going to be a performance problem? For example, let's say I have 1000 users and I'm hitting a MySQL table every 5 seconds for each user.
Can anyone suggest a better method?

With your actual architecture, your SGBD will get a heart attack :)
The solution reside on implementation of Web Socket
in back-end only 1 instance of PHP check if there is a new update on database, if there is, PHP can invoke a web service to your Web-Socket Server (like NodeJS), and Server send the message to the client

Related

Looping sql Query with ajax to update data

I'm working and creating a messenger like app now, I just want to ask
It it advisable to loop an ajax that queries data from MYSQL, to have an updated data for my messages?
I want page data to be updated every time there is a new message from the sender, the receiver will receive the data without reloading the page, and my approach for this is to loop my ajax query that gets all the messages for the receiver or sender from my sql table.
Q2. Will this affects the performance of my database?
Q3. Is there any other way to do this? I'm currently working with php now.
Thank you!
For a few users, no problem. If you have more than, say, 10K users sitting in that loop, you might start smelling smoke come out of the database server.
For large scale deployment, I think you need something "push" technology outside the realm of AJAX, PHP, and MySQL.
One AJAX call will lead to
Web server sees the request, and hands it to
PHP which starts up as a "child" to the web server; then PHP
Connects to MySQL, and
Performs a query, and
PHP replies to
AJAX, which either puts up the message or sleeps for another second;
7,8 Meanwhile, MySQL and PHP shutdown.
That's a lot of steps; I am not sure which part will be the worst. But, assuming everything is running on a single machine, there are limits to how many users it will support.

Speed up the jquery ajax call

I have a game site with some grids. More than one player can play the same game at a time.
If any user clicks a single grid, i need to convert that particular grid to active state for all the user those who are playing the game now. For that, i made the ajax call for every seconds to make the selected grids active. But it slows the process, since it access the database for every time.
Please help me to do this task in any other way without slow
Use WebSocket in that case and you don't need a database.
With WebSocket you can easily push data to all your connected clients in realtime.
And you have the nice ability to push new data directly from server to client.
The client doesn't need to send every x second a call to the server. There is a persistent connection.
A good WebSocket Library written in PHP you can find here: http://socketo.me

MySQL callback to the web-app

I'm trying to figure out the best way possible to notify my web-application of database changes. The way I'm doing it now, is that I've got separate MySQL table with a counter.
Now, when some database action happens in table Foo, a trigger is adding up the counter.
The web-app then polls every 5 seconds the server to check out, if something has happened (counter number has changed) and if so, refresh the data in app.
What I would like, is that I would be able to do callback/notify from MySQL to the server and from there to the web-app so that I don't need to poll the server frequently. Is this possible somehow?
How does facebook, gmail send the real time notification?
You can't notify your application directly from MySQL, but there are some solutions to save bandwith and load of your server.
one way of handling this - would be to either implement the observer pattern yourself or simply use a pubsub messaging option (ZMQ/AMQ/RabbitMQ/Redis etc) - when the initial database action takes place (ensure that the transaction has committed), publish the message to the topic on the pubusb tool - your application can subscribe to the pubsub tool and receive messages when there is a DB change.
Hope it helps.

web based chat application

hii.. i am designing a chat application. i have tried using php + MySQL + JavaScript and developed the web based chat app. But the application is quite slow. What all i do is, just store the messages from the clients in a table, and each client retrieves the table at a lag of 2 seconds.... I manage this by ajax.
But the app response becomes very bad when many user joins.
Is their any other technique to build the chat app ??? or what else i can do to make my app better.
help plzz ..
thanks in advance :)
Don't know if you've touched upon it, but I'd recomend using an IRC server as backend.
If you have the ability to install nodejs(basicly server side javascript) on your server you should take a look at socket.io which is a plugin for nodejs. This gives you almost an instant response time, even for quite large masses of users.
There are almost no libraries/projects for real-time web applications in PHP. But if you really want to use it you can take a look at a technique called long-polling. This can still be quite heavy on your server though.
Maybe it is slow because each client retrieves full table of chat contents - try appending only the new messages.
I've done the same thing last year.
I suggest retrieving only so many chats when the user first logs in, e.g. the last 30, and displaying them. Then on each subsequent AJAX call only retrieve any new chat messages, rather than everything else again. You can do this by storing the latest chat id (a unique id) when you first retrieve all the chat msgs and then only retrieve anything with a chat id greater than that.
How often do you check for new msgs? You could also increase that time.

Turn based web game in PHP - communication issue

I am working on a turn based web game in PHP5.
This is pretty simple game, a kind of board game: two people join a "session" and they play until one of them wins.
My problem in nutshell:
User A and User B play a game.
User A finished his turn
Request will be sent to the server to perform necessary operations.
Now it is time for User B to move..
But how could I notify User B about this?
I mean, now the server has to communicate with the other user, the one that is inactive, not the one that initiated the request.
I know that this could be implemented using some kind of periodic AJAX call that checks whether the opponent finished his turn etc, but such a thing generates a huge number of requests.
Isn't there a better way to solve this?
I'm thinking of something like this:
User A's turn ends
Server saves his score
Server contacts User B
User B's turn gets started.
Is this possible using PHP and comet-style requests somehow? Or is there a better way to do this?
Any help would be appreciated!
Thanks in advance!
An in-memory database of currently running games/turns, and one check from user B every 1.5 secs or so, won't really generate a huge number of requests, or server load.
You can even have a polling scheme like 7s, 5s, 3s, 2s, 1s, 1s and so on, according to what fits your game.
You can even leave PHP out entirely, if you just touch a session file whenever a turn is done, and check the last-modified client side.
Php Sockets !
http://php.net/manual/en/book.sockets.php
server listen
client_a connect
client_b connect
client_a send start game to server
client_a send move to server
server send play client_b
[...]

Categories