Getting php data from a database using jquery without ajax

I am developing a chat with PHP support, currently I use ajax to retrieve data from the server and display it in the chat, but this uses several ajax requests on the client computer, which makes it slow down abit ....

This is what I do using ajax and yii2

function getdata() { $.get("controller/action"). done(function(){ //process json $("#chatbox").html(data); }) } 

then i use

 windows.setInterval(getdata(),1000); 

Are there any better ways to get this son's data without using ajax and jquery

I checked this link , but its not very useful

+5
source share
5 answers

You can use socket.io or websockets api, which is an alternative for ajax. Thus, using socket.io, jquery, php OR nodejs, you can create an individual chat without using ajax, the following links will help you how to create a private chat.

socket.io

Websockets

Private chat link 1

Private chat link 2

+4
source

Better use nodejs instead of php . You can check this link for a really nice chat implementation that you can use. Although php chat has performance issues, as you mentioned, nodejs is not because instead of polling messages, it pushes them to the client when there is something to push. And also you get a turnkey solution right out of the box (of course, you have to change it), which will save you development time.

But if you still want to go with the php method, you have the following options:

  • jquery + ajax (as if 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 main drawbacks is that it is not supported by older browsers, and the setup process is not so simple. But I would prefer this over ajax requests.
+2
source

You mentioned getting data from a database, but it can be argued that the database is random for the chat application. You might want to keep your chat history, and the database is a natural place to do this, but the main function is to send messages. Thus, you use the database as a kind of message buffer.

Websockets seems like the best option, as others have mentioned. If you need the server side of PHP, in addition to the Kraken framework, as indicated in the commentary on your question, you can take a look at the Ratchet library . They have a tutorial for easy communication on their website: http://socketo.me/docs/hello-world

Basically, you need a different server process (in addition to your web server) to receive and broadcast chat messages. Following this onMessage , in the onMessage method of the Chat class, you can insert into the database, if necessary, preferably asynchronously.

On the client side, you will need to connect to the websocket using Javascript. A 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) { // } 
+1
source

You can do the trick, suppose the data is not json - this is a javascript file declaring one variable, now you have to add it to the document, for example

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); } 
0
source

There are no reasonable ways. Do you have to somehow bring in new data? To do this, reload the page or Javascript / Ajax to avoid reloading.

You can make the update one-way so that when Person A writes to Person B, the request is executed when a new message is sent. This would mean that no new messages would be received if they were not sent. (Not practical)

Another method would be to have the time of the last message marked somewhere by itself, and you can check this many times. If this is a time change, you can get new data, but it will not correct the number of requests ... only the amount of data transferred.

I suggest you look at the size of the data from json / php. Have you tested to find out how long this has been going on or how it works?

I could direct you to this post which, if you like, uses NON jquery queries.

-6
source

Source: https://habr.com/ru/post/1258641/


All Articles