I’m trying to build a “view recent searches” functionality into a website I have. Essentially, I would like to display a new recent search on the website every 5 seconds.
What is the best way to do this? I was thinking of having each page connect to my server with socket.io, then broadcast a message to all sockets every 5 seconds about new searches. On the client-side, upon receiving the message from the server on the socket, i’ll use some client-side javascript to update the recent searches part of the webpage.
Is the best way to do this? If so, how should I implement it?
I am using node.js/express.
Something like this…
(Untested, but should work. Requires Socket.IO 0.7.x or newer. Let me know how this goes
=].Do note, however, that this is not the most optimal way of handling this. It would be best if you used events to signal your server to send new data to the client. For example, when you add new data to your database, at that time (and that time only) send new data to the client. It’s a bit silly & inefficient to check every 5 seconds when you can design around it. The whole point of Socket.IO is to push, not to poll. New technologies require a new way of thinking.
Also, very important about displaying recent searches: These links are typically NEVER displayed for users, as they’re often too random to be useful. Top searches, maybe, but that information wouldn’t change regularly enough to warrant such a frequent refresh. In addition, most people use this technique to get their website listed for page for which they have no true content. An important thing to note is that if you allow search engines to these pages (by not blacklisting your search results pages in
robots.txt), you run a VERY high risk of getting a low-quality flag from Google.My most sincere advice: While this may be a cool feature to have, especially for an admin console, it is utterly useless for visitors and can prove to be a detriment to your website’s SEO value by adding to Google’s index low quality content and loads of links going nowhere useful. I wouldn’t even display it.