Best practice for storing data in web chat application - javascript

I'm building web chat application using Code Igniter. In this chat app, there is only one channel / room. I'd like to know what's the best practice to store data whether using database or file in order to save bandwith and page load.
p.s : I use javascript setInterval to load chat div every x seconds.

if you want the file you could use reverse-ajax/comet for it is faster and take lesser bandwidth cause it uses long pooling
if you use database and use ajax it is slower because it always updates for new chat message in your database get it from there take the rows and displays it which takes time and I think more bandwidth

Related

Nodejs GET REST API and frequent access to MongoDb database

I'm implementing a web app using the MERN framework (MongoDB, Express and Node.js for the back-end, React for the front-end).
In a section of my web app, I need to access a collection of the Mongo database very frequently (every 50 ms).
I need to synchronize this data with a video player.
I'd like to know which is the best way to handle this situation.
The options I came up with right now are:
Send a single GET request to the collection and save the whole content of that collection in a variable of the front-end (but I think it's the worst solution, since the size of this collection is 350MB)
Send a GET request every 50 ms
Send a GET request every N seconds based on the current time of the video player, and save the content of the request dynamically in a variable of the front-end
I'm sure there are better ways to handle this situation.
i think correct approach to achieve that is to open sockets in you application as it will poll you server automatically.here is the link socket.io

How do I push mongodb data?

So I have developed a web app as a hobby on Handlebars, Express and Mongoose/MongoDB.
The app let's users create an account and then post advertisements for other users to see and respond to.
All the ads posted by users show up on the index page. So it is common view for all the users on this web app. I am relatively new to web development so to build such a simple app it took me a while but boy I learned a lot!
Now the issue I am facing is, when a user A posts an Ad while the user B is logged in and is currently on the index page (a page that lists all the ads posted) it won't show up for user B unless user B refreshes the page. Rightly so actually because only when the index page's route is hit it will query all the ads and refreshing is basically hitting the index route I get that. But I don't want it that way. I want it to show the new ad on user B's index and pretty much every user's index if there's new ad by any user.
So I did a little research/reading and I learned that I can do it by learning to work with triggers on mongodb and like create some kind of trigger that when a new ad is posted do something. I like the idea but failed to find resources to learn how to use such a thing.
The other option I was suggested was to use socket.io but that too I can't grasp how can I make an entire Ad document work as a socket. I am lost and implementing this feature of dynamically loading ads for all users will complete this hobby project of mine and will help me find a junior dev job in local community.
I request stackoverflow's community to guide me how do I go about doing this and what resources I can use to learn about it.
The socket.io seems to be the best solution for your case. What you will want to do with socket.io is every time a user posts an Ad you use socket.io to notify the rest of the users that there is an update.
If you don't want to send the entire document using the socket you can use the socket to notify the clients and on the client side every time you receive such a notice from the server you will either
a) Refresh the page(not suggested as it will make the user experience unpleasant) which is easier to implement
OR
b) You can use an Ajax request to get the new data from your server and update the fields on the fly(which makes for a better user experience).
Best Way You can come with using Short Polling concept from client side to ask for new data after 1 or 2 seconds (whatsoever count to need ) . Gmail for new inbox mails also uses sync method in a particular fashion . Just ask from server for new data
OR Second option to go through below
On Server Side
Serve index.html page to User A (which is logged in now).Some User B inserts data
Maintain a function or a cron job (checks the count of Total Ads ) Lets say after every 1 minute or so
If there is change in count from the previous total_count , update it and get new mlab documents and send it to function , Let's say push_new_ads which will be sync via socket.io to client
On Client Side
Sync your client_total_count with server_local_count push_new_ads using socket.io and If there is change in count , make a simple fetch api call to get data and appends it to previously fetched array
There is no such way to directly listen the changes in mongodb But you can trigger some changes from oplog using tailable cursors

How to deal with database changes in a MEAN application

I have struggled to find many resources on this online. I am developing an application that multiple users will be using at the same time. This means that one user may edit the database after another user has loaded the data from database. This means that this second user will not have an up to date view of the current state of the database. What is the best way to subscribe to database changes and deal with them. I am using a MEAN stack.
If you are trying to develop a real time system where changes are reflected instantly upon changes in database, you need to make use of web sockets. Since you are using Node.js as backend, see Socket.io
A good resource for implementation can be found here
However, if you plan on implementing web sockets, you will have to make significant changes to both your Node.js and Angular code.
Another method (which I would not recommend) is to make periodic api calls for those views which you want to reflect real time changes. You can make use of setInterval for this

JS - Virtual pet evolution when user is not on application

I'm using VueJS and MongoDB to create a virtual pet.
We're saving the user data using localStorage.
I'm wondering what the mechanism would be, to make the virtual pet evolve (ie. life gauge going down) when the user is not on the web app.
Would I need to save the date when the user leaves the app ?
Yes, you should save the time when the user leaves the app.
When they return (so, whenever you fetch data from the database), compare the saved time to the current time and apply whatever operations need to happen based on the difference.
Alternatively, you could have a server always running and deal with scheduled jobs and the like to keep it all updated in realtime, but lazy evaluation that only happens when the user requests the data should suffice for this case.

MSSQL Long polling | Is calling database in while loop for new information safe?

Imagine you were starting a site that will grow to millions of users in a year. I understand that you can load balance your servers to keep thing speedy, but my question is this :
Using the long polling method for creating a live chat system and updating content on the users timeline, you give a request to a php file on the server, and call the database each cycle through the while loop while looking for new data, correct? If so, isn't that WAYYY too many times to be calling a database? Thanks.

Categories

Resources