0

For building a simplistic live search system, I trying to decide whether to use Websockets or Ajax. Implementation wise, for every letter the user types (or maybe every 0.5 seconds), a database query will be run against the input/typed text and some comparisons will be made on the query output to return the top 5-10 results to the user device.

I am very familiar with Ajax and learning Websockets will be a good exercise, if it turns out to be the better tech to use. But as I am not presently qualified to run benchmarking tests with both, I would appreciate any guidance on this. Will the connection overhead of Ajax make a significant enough difference in this case? Are there any other factors that might matter?

1 Answer 1

5

You only need websockets if you want the communication to be initiated by the server, not the client. Unless I misunderstand, in your case, the client seems to initiate every communication. In that case, Ajax is sufficient and I would advise to stick with what you already know.

You would need websockets if the underlying dataset updates (while the client isn't typing) and hence new results qualify. An example of this is Stack Exchange itself: the homepage and tag pages have websockets to notify you of newly posted questions matching your criteria. That would then be able to replace running the query "maybe every 0.5 seconds", but it requires quite a lot of work on the backend as well. You need to know which connections there are and when a new result qualifies for being pushed over the websocket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.