├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── package.json └── public ├── index.html └── js └── client.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jared Wolff 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Node.js Websocket Example 2 | ======================== 3 | 4 | ## Info 5 | 6 | This repoistory houses a minimal WebSocket example using Node.js. 7 | 8 | This repository was created for use in the following blog post: 9 | 10 | [Getting interactive using Node.js WebSockets](https://www.jaredwolff.com/blog/raspberry-pi-getting-interactive-with-your-server-using-websockets) 11 | 12 | If you use this code or found this example useful feel free to link back to the article or this Github page. 13 | 14 | ## Requirements 15 | 16 | **node.js v7.8.0** 17 | 18 | Install socket.io using included package file: 19 | 20 | `npm install` 21 | 22 | ## License 23 | 24 | MIT License: for more information see LICENSE 25 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var app = require('http').createServer(handler) 2 | var io = require('socket.io')(app) 3 | var url = require('url') 4 | var fs = require('fs') 5 | 6 | 7 | //This will open a server at localhost:5000. Navigate to this in your browser. 8 | app.listen(5000); 9 | 10 | // Http handler function 11 | function handler (req, res) { 12 | 13 | // Using URL to parse the requested URL 14 | var path = url.parse(req.url).pathname; 15 | 16 | // Managing the root route 17 | if (path == '/') { 18 | index = fs.readFile(__dirname+'/public/index.html', 19 | function(error,data) { 20 | 21 | if (error) { 22 | res.writeHead(500); 23 | return res.end("Error: unable to load index.html"); 24 | } 25 | 26 | res.writeHead(200,{'Content-Type': 'text/html'}); 27 | res.end(data); 28 | }); 29 | // Managing the route for the javascript files 30 | } else if( /\.(js)$/.test(path) ) { 31 | index = fs.readFile(__dirname+'/public'+path, 32 | function(error,data) { 33 | 34 | if (error) { 35 | res.writeHead(500); 36 | return res.end("Error: unable to load " + path); 37 | } 38 | 39 | res.writeHead(200,{'Content-Type': 'text/plain'}); 40 | res.end(data); 41 | }); 42 | } else { 43 | res.writeHead(404); 44 | res.end("Error: 404 - File not found."); 45 | } 46 | 47 | } 48 | 49 | // Web Socket Connection 50 | io.sockets.on('connection', function (socket) { 51 | 52 | // If we recieved a command from a client to start watering lets do so 53 | socket.on('example-ping', function(data) { 54 | console.log("ping"); 55 | 56 | delay = data["duration"]; 57 | 58 | // Set a timer for when we should stop watering 59 | setTimeout(function(){ 60 | socket.emit("example-pong"); 61 | }, delay*1000); 62 | 63 | }); 64 | 65 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-websocket-example", 3 | "version": "1.0.0", 4 | "description": "Websocket example using Node.js from www.jaredwolff.com", 5 | "main": "app.js", 6 | "scripts": { 7 | "default": "node app.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/jaredwolff/nodejs-websocket-example.git" 13 | }, 14 | "keywords": [ 15 | "websocket", 16 | "node", 17 | "server" 18 | ], 19 | "author": "Jared Wolff", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/jaredwolff/nodejs-websocket-example/issues" 23 | }, 24 | "homepage": "https://github.com/jaredwolff/nodejs-websocket-example#readme", 25 | "devDependencies": { 26 | "socket.io": "^1.7.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /public/js/client.js: -------------------------------------------------------------------------------- 1 | var socket = io('http://localhost:5000'); 2 | 3 | socket.on('example-pong', function (data) { 4 | console.log("pong"); 5 | }); 6 | 7 | window.addEventListener("load", function(){ 8 | 9 | var button = document.getElementById('hello'); 10 | 11 | button.addEventListener('click', function() { 12 | console.log("ping"); 13 | socket.emit('example-ping', { duration: 2 }); 14 | }); 15 | 16 | }); --------------------------------------------------------------------------------