Live chat using AJAX - ajax

Hello everyone
I just wanted to know if I use AJAX for my chat engine, will it crash my server or it will work fine for a traffic of about 1500 people at a time ??
Also suggest me some way to make automatic ajax request to update the contents of chat box. How much delay between consecutive query should be set so that it doesn't cause my server to overload ???
Thanks in advance :) :)

Depending on your requirements, you might want to look into WebSockets. At a local hacknight, we produced a simple chat system pretty quickly using http://faye.jcoglan.com/. It's a pub sub service that you can use javascript on the client side to subscribe to a specific channel. Then it's just a matter of coding the front end to work with the input and output.

Related

Sending live data to a client: Are websockets the best option (Vuex & Socket.io?

I'm trying to set up a front end UI for the webApp I've been working on and have a clarification question before proceeding further.
Right now I have multiple sensor units streaming data to the server that is saving it to a database and then set up a crude api, to be expanded on later, to interface with the server and DB.
Now, moving to the front end, I feel like I'm bashing my heading first trying to learn react and now vue. The first feature of the UI I'd like to have is to constantly see the sensor data displayed in a table on the browser.
Should I be calling this data from the server using the API http requests or have it sent using websockets?
After spending a few hours trying to get vue connected to the socket opened on the server I wanted to check if I'm just looking in the wrong direction to begin with?
Thanks!
It would be best to create a websocket connections since your data is continuously changing. You are definitely on the right track.
Just create an vuex action that will commit your data to the store. It does't really matter which websocket tool you use. Most of them work quite the same. There will be most likely a callback function which gets triggered when new data has been sent to the front-end. This is where you would call your vuex action and pass through the data. If you can edit your question with some sample code, I can help you in more detail.
Just to some it up:
1) Open Websocket connection to backend service
2) Create action which would save the data to store via mutations
3) Specify websocket callback function to call action

Is running an ajax request every second bad?

I am currently trying to create a chat function and notifications function on my site that would get the current users logged in and notifications periodically from a mysql database and right now I have it set to do an ajax call every second to a php page which returns the users online and notifications back in a json object. I am just trying to make sure there isn't a better way of doing this that I can't think of. Also is doing an ajax call to my server so often bad? I would really appreciate any insight on this. Thanks in advance!
As long as you have a hand full of chatters: no. It is not the best design, but it will do the trick. If you are looking for scalable techniques look for Comet or Web Sockets.

Socket.io - Only Emit to Certain Users?

Forewarning, I am very new to Node.js and Socket.io and this probably has a simple answer.
I am using it in conjunction with a PHP app. The intention is to have real-time notifications.
If you take a scenario such as Facebook -- something with lots of users, but the notifications should only be sent out to certain sets of users, how do I target those users?
I've thought of setting something like:
socket.on('notification-[user_id]', function() {...} );
But that would be insane considering thousands of users.
It also seems to be insane to send notifications to thousands of users and have each user have to figure out whether it's for them or not.
Is there a way around this? What am I missing?
I'm new to them too, but i think you can do it like this:
In the client side you can just:
socket.on('notification',function(){...});
In the server side you can use :
io.sockets.connected[socketid].emit('nofification',data);
Hope it can help you~

Ajax Server-Side synchronized HTTP request?

I am building a website which has a cron job that generates a file to the hard drive regularly. However, the timing of this generation is not precise and I would like it to be loaded as soon as it is generated by the browsers of my visitors.
Is there a nice way to make my server notify my visitor's browser to reload the page?
Other way around is quite heavy :(
Thanks!!
you should read about Comet, Comet allows you the receive "Push" from server-side to your client-side.
Eg,. thats the way how facebook chat works..
Take a look,
http://en.wikipedia.org/wiki/Comet_(programming)

Ajax vs. Web sockets vs. Web Workers

What is the difference between all three? They seem to do the exact same thing. Why and when would you choose to use one method over the other?
AJAX and websockets do similar tasks — they both establish a communication channel to a server. Web workers have nothing to do with either of them, they are just separate threads of JS execution.
AJAX is more mature than websockets — it has been around much longer and has a much wider browser support. AJAX is request-oriented — you make a request to the server, the server responds, and the connection is closed. Websockets on the other hand establish a persistent connection to the server, over which you exchange multiple messages in both directions.
Webworkers are useful if you want to perform a processor intensive task without blocking the browser interface.
They are not same. But one can use them together to build advanced application.
Ajax: As abbrevation States is asynchronous javascript and xml.. is used to load the content dynamically from the server upon called.
Websockets : Websockets is the feature defined in HTML5 . As wikipedia states "WebSocket is a protocol providing full-duplex communications channels over a single TCP connection." so this is mainly used for real time communication such as video call, live chat etc..
WebWorkers : this feature is also defined in HTML5. This is basically used to make bring multi threading feature in Javascript. Since javascript is a single threaded programming language , it breaks or pause whenever i.e heavy calculation tasks are done using it. to overcome this breakage , Webworkers are added to javascript.
You can perform Ajax and Websockets inside Webworkers . however you cannot manipulate DOM using webworkers due to security reasons.
They are not the same.
Ajax: It is a way of interacting with a web server asynchronously from a UI renderer
Web Sockets: An HTML5 feature using which you can interact with any Socket server extending the reach of the browser
Web Workers : Another HTML5 feature that helps you do multi-threaded programming from a web browser using Java Script
Ajax & Websockers are siblings.
Webworkers are completely different.
AJAX
The best example of AJAX is Google's search bar - suggestions appear as you type, but the current webpage is not redirected or refreshed! (10 years ago this was amazing, not so much anymore). This is AJAX in action.
AJAX uses what's called a "request" and "response" model: you ask a question, and you receive an answer from the 'server'.
AJAX allows webpages to talk to "servers" behind the scenes, allowing you to update a webpage without navigating away from your URL. Back in the old days of the web, if you wanted to show different content on a webpage, users would have to navigate to a different URL: not any more. This concept has been taken to the next level with single page apps and applications (like React, Vue, Elm etc.).
Websockets:
With websockets, your web-page talks to your server (as with Ajax), and your server responds - except you do so like you're talking on the phone. There is a "connection" between your users and your server. This "connection" is not there with AJAX: in that case, you have a simple request and a response coming back from the server.
In other words, if you wanted to stream stock market data, constantly updating it to your users: it would probably be better to use websockets, than AJAX.
Web-workers:
Use When you need intensive calculations - if you were to ask a web-page to calculate Pi to 100000 decimal places: that might take a while. The web-page might freeze, and you might lose $$. The intensive calculations can be done in the background, without freezing your webpage. People using your site can do other things - e.g. click around, while waiting for the result in the meantime.

Resources