Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('dotenv').config();
- var app = require('express')();
- var server = require('http').Server(app);
- var io = require('socket.io')(server);
- var mysql = require('mysql');
- var Redis = require('ioredis');
- var redisClient = Redis.createClient();
- /////////////////////////// Mysql Connection ////////////////////////////////
- // var con = mysql.createConnection({
- // host: process.env.DB_HOST,
- // user: process.env.DB_USERNAME,
- // password: process.env.DB_PASSWORD,
- // database: process.env.DB_DATABASE
- // });
- // con.connect(function(err) {
- // if (err) console.error.bind(console, 'DB Connection Error:');
- // else console.log("DB Connected!");
- // });
- var db_config = {
- host: process.env.DB_HOST,
- user: process.env.DB_USERNAME,
- password: process.env.DB_PASSWORD,
- database: process.env.DB_DATABASE
- };
- var con;
- function handleDisconnect() {
- con = mysql.createConnection(db_config); // Recreate the connection, since
- // the old one cannot be reused.
- con.connect(function(err) { // The server is either down
- if(err)
- { // or restarting (takes a while sometimes).
- console.log('error when connecting to db:', err.message);
- setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
- }
- else
- {
- console.log("DB Connected!");
- } // to avoid a hot loop, and to allow our node script to
- }); // process asynchronous requests in the meantime.
- con.on('error', function(err) {
- console.log('db error', err);
- if(err.code === 'PROTOCOL_CONNECTION_LOST')
- { // Connection to the MySQL server is usually
- handleDisconnect(); // lost due to either server restart, or a
- }
- else
- { // connnection idle timeout (the wait_timeout
- console.log("Mysql Error - ",err.message);
- throw err; // server variable configures this)
- }
- });
- }
- handleDisconnect();
- /////////////////////////// Mysql Connection ////////////////////////////////
- var port_number = process.env.SOCKET_IO_PORT || 8080;
- server.listen(port_number, function(){
- console.log("Listening on localhost:"+port_number)
- });
- var clients = {};
- /////////////////////////////////////// App Chat //////////////////////////////////////////////////////////
- redisClient.subscribe('message');
- redisClient.on("message", function(channel, data) {
- var tempdata = JSON.parse(data)
- //console.log("Sending: " + data);
- if(clients[tempdata.receiver_id])
- {
- io.sockets.connected[clients[tempdata.receiver_id].socket].emit('new-message', tempdata);
- }
- else
- {
- console.log("User does not exist: " + JSON.stringify(tempdata));
- }
- });
- /////////////////////////////////////// App Chat //////////////////////////////////////////////////////////
- io.on('connect', function (socket) {
- /////////////////////////////////////////////////////
- ////////////////// Add User Event /////////////////////////////////
- socket.on('add-user', function(data)
- {
- //console.log('data - ',data);
- con.query(`SELECT * FROM users WHERE id = `+data.user_id+` AND blocked='0' AND access_token='`+data.access_token+`' LIMIT 0,1 `, function (error, results, fields)
- {
- if(error)
- {
- io.sockets.connected[data.socket_id].emit('add-user-auth', {success:0, msg: error.message});
- console.log(error.message);
- }
- else
- {
- if(results.length != 0)
- {
- // console.log('New User Added - '+JSON.stringify(clients))
- clients[data.user_id] =
- {
- "socket": data.socket_id
- };
- io.sockets.connected[data.socket_id].emit('add-user-auth', {success:1, msg: 'Connected'});
- console.log('New User Added - '+results[0].id)
- }
- else
- {
- io.sockets.connected[data.socket_id].emit('add-user-auth', {success:0, msg: 'Invalid Access token'});
- console.log('Wrong Access Token - '+data.user_id);
- }
- }
- });
- });
- ////////////////// Add User Evendt /////////////////////////////////
- /////////////////////////////////////////////////////
- //Removing the socket on disconnect
- socket.on('disconnect', function()
- {
- for(var id in clients)
- {
- if(clients[id].socket === socket.id)
- {
- delete clients[id];
- break;
- }
- }
- //console.log('Connection Disconnected', JSON.stringify(clients));
- //redisClient.quit();
- });
- });
Add Comment
Please, Sign In to add comment