Saif Bechan
Saif Bechan

Reputation: 17121

Is it a bad practice to send HTML over an WebSocket connection

I am developing a small app in Node.js with a MongoDB database, I have nginx in front to handle static files and the initial index.html.

When I visit my index.html all the javascript/css/img files are loaded, and a websocket connection is made to the node.js server.

Initially my index.html is empty, it needs to be filled with the right template file. One thing I could do is do a normal ajax req.

$.get('myfile.html') and append it to my content. The websocket just handles the database data.


Now I already have an open websocket connection the server, why don't I just transfer the .html template along with the data from the database, and merge it in the client.

The template files are just little html snippets, why waste a http req, right??


Note that this app will only be used by selective people with full html5 browser support. No fallback situations are required.

Upvotes: 8

Views: 5286

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39261

Given that HTTP is so well suited for serving files, with caching -- potentially in several layers -- working for you so you won't even have to send any html content more than once to a visitor, I definitely think the upsides outweighs the downsides. It sounds like you could send an index.html along with related content, and have the templates in your index.html (e.g. as in jQuery templates which can use a <script id="fooTemplate" type="text/x-jquery-tmpl"> tag).

Obviously you can send the templates over a WebSocket.

Upvotes: 7

Related Questions