Created
          January 7, 2015 13:53 
        
      - 
      
 - 
        
Save matchs/b9d915ee29110388bf56 to your computer and use it in GitHub Desktop.  
    small test of a nodejs push service made back in 2013
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <!doctype html> | |
| <html> | |
| <head> | |
| <title>Socket.io Test</title> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script> | |
| </head> | |
| <body> | |
| <script> | |
| var socket = io.connect(); | |
| socket.emit('access_token','00291BE8-54BC-DE62-A450-A9A9AFB39124'); | |
| socket.on('noti_nova_mensagem', function (data) { | |
| console.log('Recebi',data); | |
| $('#date').text(data.mensagem); | |
| }); | |
| </script> | |
| <div id="date"></div> | |
| <textarea id="text"></textarea> | |
| </body> | |
| </html> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** Bibliotecas nativas **/ | |
| var url = require('url'); | |
| var fs = require('fs'); | |
| exports.server = function (request, response) { | |
| var path = url.parse(request.url).pathname; | |
| fs.readFile(__dirname + '/../test.html', function (err, data) { | |
| response.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'}); | |
| response.write(data, 'utf8'); | |
| response.end(); | |
| }); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | exports.socket = function(socket){ | |
| socket.on('access_token', function(access_token) { | |
| console.log('') | |
| connections[access_token] = socket; | |
| }); | |
| console.log('connected to sockect'); | |
| client.on('notification',function(msg){ | |
| console.log('notificado',msg); | |
| socket.emit('nova_mensagem', {'mensagem': msg}); | |
| }); | |
| var query = client.query("LISTEN noti_nova_conversa"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "name": "push.api", | |
| "description": "A simple push notification service written in Node.js", | |
| "author": "matchs", | |
| "dependencies": { | |
| "socket.io" :">= 0.9.x", | |
| "express" : ">= 3.2.x", | |
| "mongoose" : ">= 3.6.x", | |
| "pg" : ">= 1.1.x", | |
| "redis" : ">= 0.8.x", | |
| "hiredis" : ">= 0.1.x" | |
| }, | |
| "engine": "node >= 0.8.x" | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** Bibliotecas nativas **/ | |
| var http = require('http'); | |
| /** Dependências **/ | |
| var pg = require('pg'); | |
| var redis = require('redis'); | |
| /** Arquivos de configuração **/ | |
| var database = require('./configs/database.json'); | |
| /** Módulos da aplicação **/ | |
| var handler = require('./modules/server').server; | |
| var socketHandler = require('./modules/socket').socket; | |
| pg.connect(database.postgresql.connectionString, function (err, client, done) { | |
| if (!err) { | |
| var server = http.createServer(handler); | |
| server.listen(8001); | |
| var redisClient = redis.createClient(database.redis.port, database.redis.host, {}); | |
| var io = require('socket.io').listen(server); | |
| io.sockets.on('connection', function (socket) { | |
| socket.on('access_token', function (access_token) { | |
| console.log('connection with access_token', access_token, socket.id); | |
| redisClient.mset(['access_token:' + access_token, socket.id], function (err, resp) { | |
| if (!err) | |
| console.log('redis key', 'access_token:' + access_token, resp); | |
| else | |
| console.log('redis error', err); | |
| }); | |
| }); | |
| }); | |
| var query = client.query("LISTEN noti_nova_conversa"); | |
| client.on('notification', function (msg) { | |
| var data = JSON.parse(msg.payload); | |
| var tokens = data.tokens.split(';'); | |
| for (var i in tokens) { | |
| redisClient.get('access_token:' + tokens[i], function (err, socketId) { | |
| if (socketId){ | |
| console.log('socket found',socketId); | |
| io.sockets.sockets[socketId].emit('noti_nova_mensagem', {'mensagem': data.core_tx_mensagem}); | |
| } | |
| }) | |
| } | |
| }); | |
| } else { | |
| console.log('Error connecting to database', err); | |
| } | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment