kyle
kyle

Reputation: 157

Using socket.io to connect to mongodb

I can't seem to wrap my head around how to submit front end (express) data through socket to mongodb. I have one simple database with one collection called 'playlist' and two tables called 'youtube_id' and 'upvotes'

The following is in my app.js

 io.on('connection', function(socket){
    socket.on('insertSong', function(song, upvote){
            mongo.connect(MONGO_URL, function(err, db) 
                {
                var playlist = db.collection('playlist');
                playlist.insert({youtube_id: song, upvotes: upvote});

                });
            });


  });

and here is the code in my index.html

    <script>
                $(function(){
                        var playlist = io();
                        var song = "PEGccV-NOm8";
                        var upvote = 1;
                        playlist.emit('playlist', {song,upvote});


                });
    </script>

Upvotes: 2

Views: 2208

Answers (1)

C.Dumange
C.Dumange

Reputation: 128

You are emiting a 'playlist' event in your client and waiting for a 'insertSong' on your server.

Upvotes: 2

Related Questions