0

Right now I'm using long-polling for my web chat app. The reason why I don't use sockets is because of legacy browser compatibility issues. I'm also refraining from NodeJS because my server don't allow long processes running (it's a shared server). However, I want to improve my chat response time without continuously sending AJAX signals to the database.

I thought maybe if we can make the mysql database trigger a response to the server, that would be great. Is it possible, or nah? Why/How? Thanks!

John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67
  • If you're inserting data to the DB from your server already, doesn't it already know that it's inserting data? Why do you need the DB to tell the server again? Do you mean you want the DB to notify the client? – univerio Jun 20 '14 at 01:13
  • @univerio yup, I want it to notify the server that data has been inserted – John Evans Solachuk Jun 20 '14 at 01:39
  • I don't think you answered my question. Let me rephrase it: why do you want the database to notify the server, *when the server already knows that something has been inserted*? – univerio Jun 20 '14 at 01:47
  • @univerio opps sorry, I meant client* not server. – John Evans Solachuk Jun 20 '14 at 01:49
  • It is not possible for the database to notify the client. It sounds like either you're not actually doing long-polling (and instead just doing polling) or you're doing polling on the server. Do you periodically *poll the database* to check if you have any new messages? – univerio Jun 20 '14 at 01:54
  • @univerio `updateMessages(){ $.ajax(other datas... complete: function() { window.setTimeout(updateMessages, 2000); } }); }` Well, that is my stripped down code. Isn't that long-polling? I got it from [here](http://stackoverflow.com/questions/24115780/long-polling-in-laravel-chat-why-is-the-div-not-updating-itself) – John Evans Solachuk Jun 20 '14 at 02:01
  • 1
    No, that's just normal polling. The key is the 2-second timeout. In long-polling the timeout would usually be 0 while the *server* does the waiting. See [here](http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet) for the differences. – univerio Jun 20 '14 at 02:09
  • @univerio wow thanks mate! Can't believe I've been tricked all these while lol~ – John Evans Solachuk Jun 20 '14 at 02:16

2 Answers2

1

Unfortunately, not. The database is passive/reactive. It can perform actions, such as sending emails, but those are exceptional cases and not the norm.

If you want to make something happen when a specific thing is added to the database then the best place to add it is to the data layer which is inserting the data into the database.

Databases are not involved directly with long pulls, since they don't intercept the web request. Try moving your logic to the middle tier and having a caching mechanism there to interact with your clients.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
0

Are you really seeing any performance problems that would warrant you wanting to change the polling mechanism? The database isn't able to contact a client, its job is to be a data store.

IMHO, applications not suffering from performance problems polling is a great approach. Anything else you are going to try is most likely going to add an extra layer to the application that you probably don't want to have to manage now.

Sorry for the "answer" but I gotta build the points to be able to comment :)

Matt D.
  • 571
  • 3
  • 6
  • Haha nah, I don't think my app is facing performance issues but it hurts my eyes and heart to see AJAX signals being sent every 2 seconds. I mean, it seems quite a waste of signals to me lol. btw, what do you mean by adding an extra layer? – John Evans Solachuk Jun 20 '14 at 01:45
  • Adding in a Memory Cache to hold undelivered messages, this would help prevent so many calls directly to the database. Or maybe using a message queue to store the undelivered messages that you could in turn poll. This questions has lots of good info in it: http://stackoverflow.com/questions/5313641/scaling-a-chat-app-short-polling-vs-long-polling-ajax-php – Matt D. Jun 20 '14 at 12:32