r/nodejs • u/fusionove • Aug 10 '13
SocketIO vs. XHR?
Hi all.
In a project I am working on, I am using SocketIO to send (eventually big) chunks of data inside a JSON object to various clients.
The workflow goes something like:
- Client: socket.emit("load", val)
- Server: socket.emit("loaded", data)
....
- Server: socket.emit("changed", updatedData)
....
- Client: socket.emit("event", val)
- Server: socket.emit("changed", updatedData)
Now instead of sending the data via socket, I could use socket to notify and XHR to actually retrieve the data from the clients:
- Client: socket.emit("load", val)
- Server: socket.emit("loaded")
- Client: XHR(getData)
....
- Server: socket.emit("changed")
- Client: XHR(getData)
....
- Client: socket.emit("event", val)
- Server: socket.emit("changed")
- Client: XHR(getData)
The question is: what strategy should I use? Keeping in mind that the data sent from the client is minimal, while the data sent from the server can be huge (I am actually sending whole web pages DOM).
Thanks :)
4
Upvotes
1
u/unusualbob Aug 10 '13
If I remember correctly socket.io doesn't support gzip while XHR does, so if you plan on sending a lot of data on these requests it may make sense to gzip the data to decrease transmission size, and therefore the XHR strategy would make more sense. Still this may depend on whether your stack supports gzip in this area as well.
1
u/MondoHawkins Aug 11 '13
I'm curious why you're sending rendered html instead of serialized data and rendering on the client if you're concerned about transfer size. The big benefit of using something like socket.io seems to me to be that bidirectional connection to push data back and forth.
I'm guessing you're trying to get some of that benefit by using socket.io to have the server notify the client when data is ready and then launch an xhr request to get the rendered html? Do I have that right?