简体   繁体   中英

SetInterval mySQL query every second

I would like to implement a very small and simple group chat into my website without using any 3rd party libraries or anything.

Therefore I thought of using jQuery Ajax and Javascript's setInterval function.

On page load I am going to have a div, with all the chat messages inside, that have been posted so far + a hidden input with the max(id) of the chat-table in the database. Then I would start setInterval with an ajax request to the database every second, which returns all new messages (id > the one in the hidden input), adds them to the DOM and updates the hidden input to the new max(id).

Furthermore I thought of deleting all messages older than 48 hours from the database, to keep the chat-table very small.

Do you have any concerns about this? Will it significantly impair the performance of the site?

The site may have up to 100 concurrent users at a time, but only half of them will have access to the chat.

There are a couple things to take notice with this:

The front end (your jQuery code) can't talk to the database. You Need a server set up. since you are using Javascrpt/jquery in the front end I suggest node, it's the same language so you won't have to learn another language.

There is a lot of things you'll need, a server (i suggest node(express)), web socket exposure(I suggest socket.io), a database (I suggest redis for simplicity).

without using 3rd party libs is just not an option, even simple chats are a lot more complex and difficult than originally thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM