Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
I've recently been messing around with Node.js and Express, and decided to make a super customizable analytics program for Node.js. I'm sure such a thing already existed, but I figured it would be something fun to do to learn about websockets and nodejs libraries. Anyways, I've gotten a MySQL database working, with a table for each "apikey/user" and one command in the library. Currently I was using it with a discord bot, so I created a newMessage() command that sends the message, the author's info, and the timestamp to the backend, which then puts it into the DB.

Anyways, I'm gonna make a dashboard where the user can create custom analytics types that they can use with the library.

Oh yeah, and I'm calling it JsDoctr for some reason, and using heroku for hosting.

Github: https://github.com/Unicorn808/JsDoctr

Some code:
Used in a Discord bot

Code:

const jsDoctr = require('./index.js');
const Discord = require("discord.js");
const bot = new Discord.Client();
const token = 'superSekretToken';
const apikey = '05b51f3dafe2463390ae90d08b77a545';
jsDoctr.connect();

bot.on("message", msg => {
   if (msg.content.startsWith('!')) {
      let messageAuthor = {
         username: msg.author.username + '#' + msg.author.discriminator,
         id: msg.author.id
      };
      jsDoctr.newMessage(jsDoctr.DiscordBot, msg.content, msg.createdTimestamp, messageAuthor, apikey);
   }
});


The Library

Code:

const WebSocket = require('ws');
let wsOpen = false;
let ws;
const heroku = 'jsdoctr.herokuapp.com';
const local = 'localhost:8080';
module.exports = {
   /**
    * @return {string}
    */
   DiscordBot: function () {
      return 'DiscBot';
   },
   newMessage: function (type, content, time, author, apikey) {
      let message = {
         type: type,
         stuff: {
            content: content,
            time: time,
            author: author
         },
         key: apikey
      };
      if (wsOpen === true) {
         ws.send(JSON.stringify(message), function ack(err) {
            if (err) {
               if (err.startsWith('Error: WebSocket is not open:')) {
                  console.log('Disconnected from server, reconnecting...');
                  ws = new WebSocket('ws://' + local);
               } else if (err) {
                  console.log('Unknown Error:', err);
               }
            }
         });
      } else {
         ws.on('open', function open() {
            ws.send(JSON.stringify(message));
         });
      }
   },
   connect: function () {
      ws = new WebSocket('ws://' + local);
      ws.on('message', function incoming(data) {
         console.log('Server: ' + data);
      });
      ws.on('open', function open() {
         wsOpen = true;
      });
   }
};


Sorry about the wall of text Razz

Let me know if I'm doing anything super wrong!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement