how to make a crud application with websockets?

i want to make an express app with socketio, a simple crud app but real-time

socket.on('users:create',function(data){
    console.log(data);
    salt = cryptom.salt(32);
    hash = cryptom.hash(data.pass,salt);

                    u = new user({  name: data.name, 
                                    salt: salt,
                                    hash: hash,  
                                    email: data.email, 
                                    ci: data.ci, 
                                    telf: data.telf, 
                                    rif: data.rif, 

                                });
                    u.save(function (err) {
                        if(err){
                            console.log(err);

                        }else{

                            user.find({},function(err,user){
                                io.sockets.emit('users:index',user);
                            });

                        }});

  })

i want to know if this is correct or not, if is efficient send all the info through websockets.

Upvotes: 3

Views: 3687

Answers (1)

DF_
DF_

Reputation: 3983

You can do whatever you like. Websockets have a smaller overhead than HTTP headers so in that regard they are efficient and their main purpose is to send data across the wire as efficiently as possible!

If you're asking if your solution is correct with regards to a normal REST API then it depends on what you want to build. HTTP REST apps are built when events are not needed and the app doesn't need to change in real time. The advantage is that you can use the different HTTP methods to perform different tasks on the same route (i.e. DELETE method on user would delete the user).

You have said that your app is built to be for real-time events, then it seems that websockets would be suitable for you. It really doesn't matter though as there are ways to do both with both architectures.

Upvotes: 5

Related Questions