Geoff
Geoff

Reputation: 6649

Getting php data from database using jquery without ajax

I am developing a query PHP enabled chat, currently am using ajax to pull data from the server and display it in the chatbox but this makes use of multiple ajax requests to the client computer which causes it to slow abit....

This is what I am doing using ajax and yii2

 function getdata() {
    $.get("controller/action").
    done(function(){
       //process json

        $("#chatbox").html(data);
    })
  }

then am using

windows.setInterval(getdata(),1000);

Are there better ways to fetch this son data without using ajax and jquery

I have checked on This link buts its not very helpful

Upvotes: 5

Views: 2377

Answers (5)

alepeino
alepeino

Reputation: 9771

You mention getting data from the database, but one could argue that, for the purpose of a chat application, the database is incidental. Maybe you want to store the chat history and the database is a natural place to do so, but the primary functionality is to transmit messages. So you are using the database as some kind of message buffer.

Websockets seems the best option, as others have mentioned. If you want PHP server-side, in addition to the Kraken framework as mentioned in a comment to your question, you can take a look at the Ratchet library. They have a tutorial for a simple chat in their site: http://socketo.me/docs/hello-world

Basically, you need another server-side process (in addition to your webserver) to receive and broadcast the chat messages. Following that tutorial, in the onMessage method of the Chat class you can do the insert in the database if needed, preferably asynchronously.

Client-side, you will need to connect to the websocket using Javascript. Simple example:

var conn = new WebSocket('ws://localhost:8080');

conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log('Message received: ' + e.data);
    addMessageToChatbox(e.data);
};

$('#yourChatInput').keypress(function(e) {
    if(e.which == 13) { // "enter" was pressed
        conn.send($(this).val());
        $(this).val('');
    }
});

function addMessageToChatbox(message) {
    //
}

Upvotes: 1

himeshc_IB
himeshc_IB

Reputation: 863

You can use socket.io or websockets api which is an alternate option to ajax, So, by using socket.io, jquery, php OR nodejs one can build one to one private chat without using ajax, following links will help you out how to build private chat.

socket.io

WebSockets

Private chat reference 1

Private chat reference 2

Upvotes: 5

anshuVersatile
anshuVersatile

Reputation: 2068

You can do a trick, suppose data is not json it is javascript file declaring single variable now you have to add it to document such as

below is your data.php(javascript generated by php)

in php

echo 'var x = {"name":"Anshuman"}'

In javascript

  var s = document.createElement( 'script' );
  s.setAttribute( 'src', 'data.php');
  s.onload=callback;
  document.body.appendChild( s );
function callback(){
console.log(x);
}

Upvotes: 0

krasipenkov
krasipenkov

Reputation: 2029

A better approach is using nodejs instead of php. You can check this link for a really nice implementation of chat which you can use. While php chat has performance issues like you mentioned, nodejs doesn't because instead of polling the messages it pushes them to the client when there is something to push. And also you receive ready to use solution right out of the box (of course you have to modify it) which will save you development time.

But if you still want to go with the php way, then you have these options:

  • jquery + ajax (like you are doing it right now)
  • php sockets - here is an example of php chat using websockets https://www.sanwebe.com/2013/05/chat-using-websocket-php-socket. This approach has its pros and cons. One of the major cons is that is not supported by old browsers and may be the setup process is not that easy. But I'll prefer it over the ajax requests.

Upvotes: 2

LordRampantHump
LordRampantHump

Reputation: 9

There aren't any sensible ways. You have to bring the new data in somehow right? The to ways to do that are by reloading the page or by Javascript / Ajax to avoid the reload.

You could make the update one sided so that when Person A writes to person B the request is performed on the submit of the new message. This would mean that no new messages are retrieved unless one is sent. (Not practical)

Another method would be to have a last message time noted somewhere on its own and you could repeatedly check for that. Should that time change you could fetch new data then but that would not fix the amount of requests... only the amount of data being transferred.

I suggest you look at the size of the data from the json/php. Have you ran tests to see how long that is taking or how it is performing?

I could refer you to this post which is using NON jquery requests if you like.

Upvotes: -6

Related Questions