This may sound counter intuitive considering the nature of web sockets, but I have a need to transmit numerous repetitive socket emits to the client. Once finished, I want to load the HTML that displays the messages to the client. I'm not quite sure how to go about this, or if it's the wrong idea.
The scenario, more specifically is that I am scraping meta tags from various submitted URLs, writing the image meta property value to a dadabase, ordering them by some criteria, then emitting all of the images to the client to display them.
What I found is happening is images that take longer to load are screwing up the order they should be displayed in, despite the correct order with the socket emits. Since the image load time is outside of my control, I'm looking for ways to still accept the socket messages, but pre-load the images before displaying all of the content.
For example:
Server
var data == //json data of all my img URLs with a 'postdate'
var sortJsonArray = require('sort-json-array');
sorted = sortJsonArray(data, 'postdate', 'asc')
sorted.forEach(function(obj) {
socket.emit(image, obj.image)
}
Browser
socket.on(image, function(img){
var $div = $("<div/>")
.html('<img src="'+img+'"</>')
$('#picture').prepend($div); // because of image load time, the results are out of order
})
Can I do something with $document.ready() here?
I've reviewed the following stack but because I'm using sockets, my use-case is different and it doesn't seem to apply: Official way to ask jQuery wait for all images to load before executing something