How chat applications work without repeated ajax call? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I tried creating a chat application in Php and i used the below logic.
1.Get input from user-1 and insert it into the table.
2.Run an ajax function every 5 seconds to look for new entries in the table and display to user-2.
3.vice-versa.
This procedure is really too very slow and the browser hangs often.
How does the real-time chat application actually works ? Can the database trigger an event automatically in the front-end so that we need not hit the database every 5 seconds? If yes, how it is done?
Thanks.

That strategy is called polling and it served us good in the early 2000s, but it has done its time.
A better enhancement is long-polling. We request a webpage, but the server doesn't immediately respond but waits until there's new information available. When the clients receives the new information, it immediately sends a new request and so forth.
Now in 2015, we have HTML5 Websockets and HTML5 SSE (Server Sent Events), that allows both the client and the server to send data whenever data (on either side) is available.
You might consider using node.js (javascript) or Ratchet (PHP) on your server.

Or just long polling, like facebook...

You can use Websockets, they are supported natively in modern browsers.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
And use a PHP library to use them server side : http://socketo.me

Related

Send data continuously from flask api to javascript frontend [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I want to make a website which visualize in real time some simulations which take some time to compute and I would like to know the easiest way to do it.
For now, the javascript front-end get the final result in once from the flask api and show it to the user. I would like the backend to send the result of the simulation in real-time, with batch of data at time intervals while it still computes, and the frontend to display the results in real time without having to re-load the page each time. How could I do that easily ?
Thanks
There are two common methods for doing this:
Web Sockets
Server-Sent Events (SSE)
If the frontend of your application needs to speak to the backend very often, then it would be best to open up a web socket for this channel of communication. Here is a list of popular frameworks for commonly used languages, including Python: https://github.com/facundofarias/awesome-websockets
If the frontend of your application just needs to listen for updates, then it may be better to use open up an event stream with SSE. Note that this can be done nowadays in most browsers in plain JavaScript at the frontend, but the backend may be a little more involved. Since you're using Flask, you might want to take a look at Flask-SSE.

Website building, ask server to run a script with input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm quite a newbie in everything related to web development, but I had this project in my mind that I really wanted to realize.
At the moment I am struggling with a method to send a request to the server hosting the website. The idea is that from the site you can enter keywords, and those keywords should be run in a script (python or java(script)) on the server after which it returns an output to the webpage. In theory this script could be executed in the website itself as javascript, but I would like to use the computing power and internet access from the server.
I have researched on using Json and javascript for the job, but I think there has to be a better way for this purpose. Is there anyone that can direct me where to look?
You should build your form in HTML. (More info: https://www.w3schools.com/html/html_forms.asp)
In client-side JavaScript (the code that website users have executing in their browser), you should use the jQuery library to scoop the data out of your form. Take a look at the .submit() and .val() functions to get started. Then you can use jQuery's AJAX function to submit requests to the server-side API in a JSON format.
On the server, in addition to creating a way of serving up the webpages (with their client-side JavaScript), you will then need to create an API to receive and respond to these requests. One way to do this is by running Node.js with Express, which will allow you to write your API in JavaScript. The downside to this is you will have to pay to get a full-fledged virtualized cloud server through AWS, Rackspace, or another host. The other option is for you to write your API in PHP, which is supported by free webhosts like x10hosting.com.

Real time notification in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating a PHP application, where I want one user make a database entry. It should be reflected to all the users whoever have this app open on their browser will get instant notification that some data has been updated on the database or with whatever data has been inserted.
Is there any way I can get real time notification of data inserted or updated? On all the opened browser without any delay.
We use AJAX for now. But it seems like our application is now working slow due to continues request. Any other way? Which is free.
Thank you!
Take a look at Pusher (https://pusher.com/). It does what you want to achieve and it works like a charm.
I don't know what do you use on your server side but even if it isn't Laravel, take a look at Jeffrey's Laracast about implementing real time notifications in Laravel (PHP). https://laracasts.com/lessons/pusher-awesomeness
Have a fun! When I did it, my app became extremely sexy. Real time notifications, DOM manipulations etc.
You can use web sockets. One existing library example for PHP is Ratchet.

Live update message system using php js msyql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Okay guys, I'm trying to integrate a facebook-like messaging system into my web application. I can't post the link unfortunately because it requires login.
I have the database with a conversation table, a messages table, and the html page which calls all the messages based on the given conversation id. I also have a text area and submit button for sending a new message and adding it to the database.
Now I've heard two different methods..
That I should have javascript check for updates every second or so to see new messages, and update with ajax. Would this put too much strain on a server?
or.
To use a frame work like node.js. This method makes very little sense to me as I really don't understand requests very well.
So stack overflow, which method (or neither) would be the best way to implement this?
Polling is resource-heavy. You should consider something like WebSockets.
With WebSockets, you basically will have the client chat user apps subscribe the to the WebSocket, and then the back end server application would notify them when they should check for updates, or just push the new message updates out to them right over the WebSocket connection.
Here is a demo chat application you can look at. Here's the WebSocket website.

Ajax to notify about variable change on server [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm using Django and JQuery. I basically want to refresh team's page when the game starts (a game is started manually so the exact time is uknown). So I send an ajax request to the server asking whether the game started, but I have to do it continiously, so I'm not sure what's better:
1. Have a javascript while loop that keeps sending a request
2. Have a while loop on the server side that waits to respond until the game starts.
Or maybe there is some other better way?
Thanks!
Approach 1 which you specify is called polling and which will generate multiple calls to server so it is not recommended.
Approach 2 is not feasible since you will have time out.
Right way(efficiently) to do this type of thing is via WebSockets or some kind of Push Notification from server. few libraries are
http://socket.io/
https://github.com/SignalR/SignalR/wiki/Faq
If this had helped you don't forgot to mark it as answer :)
Your server knows when the game will start, like 5pm? Tell the browser's JavaScript to start polling at 4:59:40
Polling is simplest option since it will work with plain Django system.
There exists several other techniques to keep connection open and send messages to client (browser), like websockets or server sent events (sse), but those require additional components in infrastructure than just Django.
And you can't keep server waiting, browsers and frontend webservers usually do have time limits to detect dead connections so your connection would be terminated before game starts.

Categories

Resources