Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to use socket.io for most of the requests or should I only use it to push data to client?

Is it bad to replace AJAX routes (called with $.Ajax from jquery) such as:

  • GET /animals
  • GET /animals/[id]
  • POST /animals

With socket.io events (event binded on client and server so the client can have a response):

  • emit("animals:read")
  • emit("animals:read", {id:asdasd})
  • emit("animals:write", animalData)

or should I "only" use socket.io it to push data to client?

[EDIT] I could see one problem if I don't use socket io for my POST route. The problem is that I can't easely use the client's socket to broadcast data:

Server:

on("animals:write", function(data){ 
    saveAnimal(req.data)
    socket.broadcast(...)
    emit("animals:write", writenAnimal)  
})

VS

app.post(function(req,res){
    saveAnimal(data)
    // cant broadcast :(
    res.send(201,writenAnimal)
})

I will push data to clients in some other requests for sure, so all clients will have at least 1 socket.

like image 862
Jean-Philippe Leclerc Avatar asked Nov 07 '25 22:11

Jean-Philippe Leclerc


2 Answers

IMHO socket.io should be used if you want real-time data provided for your website. Take for example Stackoverflow. It uses websocket to update in realtime your scores and update your notifications.

But if you really want to create application that is SEO-friendly ( I mean supporting http for serving your pages ) and more important if you are aware of the difficulties in managing sessions and permissions in socket.io, I think you'll prefer AJAX for your pages and socket.io for other realtime data.

like image 170
drinchev Avatar answered Nov 10 '25 10:11

drinchev


I would use ajax for this, its http based data requests, nothing realtime.

like image 23
ThomasReggi Avatar answered Nov 10 '25 12:11

ThomasReggi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!