├── .gitignore ├── README ├── client.html ├── example.xml ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a simple example using nodejs to build a push notification server. 2 | You can read the tutorial on my blog http://www.gianlucaguarini.com/blog/?p=272 3 | 4 | Tested on node 0.12.0 5 | 6 | /** 7 | * 8 | * Version: 1.0.0 9 | * Author: Gianluca Guarini 10 | * Contact: gianluca.guarini@gmail.com 11 | * Website: http://www.gianlucaguarini.com/ 12 | * Twitter: @gianlucaguarini 13 | * 14 | * Copyright (c) Gianluca Guarini 15 | * 16 | * Permission is hereby granted, free of charge, to any person 17 | * obtaining a copy of this software and associated documentation 18 | * files (the "Software"), to deal in the Software without 19 | * restriction, including without limitation the rights to use, 20 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 21 | * copies of the Software, and to permit persons to whom the 22 | * Software is furnished to do so, subject to the following 23 | * conditions: 24 | * 25 | * The above copyright notice and this permission notice shall be 26 | * included in all copies or substantial portions of the Software. 27 | * 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 30 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 32 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 33 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 34 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 35 | * OTHER DEALINGS IN THE SOFTWARE. 36 | **/ 37 | -------------------------------------------------------------------------------- /client.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | Push notification server 10 | 11 | 12 | 13 |
Try to change your xml data to update this content
14 | 15 | 16 | 28 | 29 | -------------------------------------------------------------------------------- /example.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello world! 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-push-notification-server", 3 | "version": "1.0.1", 4 | "description": "This is a simple example using nodejs to build a push notification server.", 5 | "main": "server.js", 6 | "dependencies": { 7 | "socket.io": "~1.3.5", 8 | "xml2json": "~0.6.1" 9 | }, 10 | "devDependencies": {}, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1", 13 | "start": "node server.js" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/GianlucaGuarini/nodejs-push-notification-server.git" 18 | }, 19 | "author": "Gianluca Guarini", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/GianlucaGuarini/nodejs-push-notification-server/issues" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | var app = require('http').createServer(handler), 2 | io = require('socket.io').listen(app), 3 | parser = new require('xml2json'), 4 | fs = require('fs'); 5 | 6 | // creating the server ( localhost:8000 ) 7 | app.listen(8000); 8 | 9 | console.log('server listening on localhost:8000'); 10 | 11 | // on server started we can load our client.html page 12 | function handler(req, res) { 13 | fs.readFile(__dirname + '/client.html', function(err, data) { 14 | if (err) { 15 | console.log(err); 16 | res.writeHead(500); 17 | return res.end('Error loading client.html'); 18 | } 19 | res.writeHead(200); 20 | res.end(data); 21 | }); 22 | } 23 | 24 | // creating a new websocket to keep the content updated without any AJAX request 25 | io.sockets.on('connection', function(socket) { 26 | console.log(__dirname); 27 | // watching the xml file 28 | fs.watchFile(__dirname + '/example.xml', function(curr, prev) { 29 | // on file change we can read the new xml 30 | fs.readFile(__dirname + '/example.xml', function(err, data) { 31 | if (err) throw err; 32 | // parsing the new xml data and converting them into json file 33 | var json = parser.toJson(data); 34 | // send the new data to the client 35 | socket.volatile.emit('notification', json); 36 | }); 37 | }); 38 | 39 | }); --------------------------------------------------------------------------------