title: How to pass data to Mongodb using Node.js, websocket and total.js
body: <p>I am trying to pass data to Mongodb using Websocket and total.js.
  In my <code>homepage.html</code> I can get the user input and connect to the server via websocket after clicking the save button.</p>
  
  <p>In <code>default.js</code> is my server side code. At this point the app hat got the user input and connected to the server correctly, but how can I save data to mongodb now? </p>
  
  <p>This is my <code>homepage.html</code>     </p>
  
  <pre><code>&lt;br /&gt;
  &lt;div&gt;
  &lt;input type="text" name="message" placeholder="Service" maxlength="200" style="width:500px" /&gt;
  &lt;button name="send" &gt;Save&lt;/div&gt;
  &lt;/div&gt;
  &lt;br /&gt;
  
  &lt;script type="text/javascript"&gt;
    var socket = null;
    $(document).ready(function() {
      connect();
      $('button').bind('click', function() {
  
          if (this.name === 'send') {                     
              console.log(send());
              return;
          }           
  
      });
  });
  function connect() {
      if (socket !== null)
          return;
      socket = new WebSocket('ws://127.0.0.1:8000/');
  
      socket.onopen = function() {
          console.log('open');
      };
  
      socket.onmessage = function(e) {
          var el = $('#output');
          var m = JSON.parse(decodeURIComponent(e.data)).message;
          el.val(m + '\n' + el.val());
      };
  
      socket.onclose = function(e) {
          // e.reason ==&gt; total.js client.close('reason message');
          console.log('close');
      };
  }
  
  function send() {
      var el = $('input[name="message"]');
      var msg = el.val();
  
      if (socket !== null &amp;&amp; msg.length &gt; 0)
          socket.send(encodeURIComponent(JSON.stringify({ message: msg })));
  
      el.val('');
      return msg;
  }
  </code></pre>
  
  <p></p>
  
  <p>This is my <code>default.js</code></p>
  
  <pre><code> exports.install = function(framework) {
   framework.route('/', view_homepage);
   framework.route('/usage/', view_usage);
   framework.websocket('/', socket_homepage, ['json']);
  
   };
  
   function view_usage() {
   var self = this;
   self.plain(self.framework.usage(true));
   }
  
  function view_homepage() {
  var self = this;
  self.view('homepage');
  }
  function socket_homepage() {
      var controller = this;
      controller.on('open', function(client) {
      console.log('Connect');    
  });
  
  controller.on('message', function(client, message) {
      console.log(message);
   /*
   var self = this;
   var message = MODEL('message').schema;
   var model = self.body;
   var message = new message({ message: model.message }).save(function(err) {
      if (err)
          self.throw500(err);
  
      // Read all messages
      message.find(self.callback());
  });   
  */      
  });
  controller.on('error', function(error, client) {
      framework.error(error, 'websocket', controller.uri);
  });
  }
  </code></pre>
  
  <p>Any help Please!!!</p>
  
  <p><a href="https://github.com/samd90/websocket.git" rel="nofollow">This is complete project</a></p>
  
  <p>---Update---</p>
  
  <p>In this function i use to save data to MongoDB
  but it didn't give any error.also Didnt save the data to database.i not sure my code is write or wrong</p>
  
  <pre><code>  controller.on('message', function(client, message) {
      console.log(message);
   /*
   var self = this;
   var message = MODEL('message').schema;
   var model = self.body;
   var message = new message({ message: model.message }).save(function(err) {
      if (err)
          self.throw500(err);
  
      // Read all messages
      message.find(self.callback());
  });   
  */      
  });
  </code></pre>
  
  <p>This my mongoose.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  mongoose.connect('mongodb://totaldemo:123456@ds029979.mongolab.com:29979/totaldemo');
  global.mongoose = mongoose;
  </code></pre>
  
  <p>This is my user.js</p>
  
  <pre><code> var userSchema = mongoose.Schema({ user: String})
   exports.schema = mongoose.model('user', userSchema,'user');
   exports.name = 'user';
  </code></pre>