├── .gitignore ├── .npmignore ├── README.md ├── demo.gif ├── index.js ├── package.json └── src └── Demo.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | demo.gif 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | This is a demo for [rockets](https://github.com/rtheunissen/rockets). 3 | 4 | --- 5 | 6 | ```bash 7 | npm install rockets-demo 8 | cd node_modules 9 | cd rockets-demo 10 | npm start # or `node index.js` 11 | ``` 12 | 13 | --- 14 | 15 | 16 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rockets/demo/61e01873a991e9a0d5ae2221035245e8f3b12187/demo.gif -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('colors'); 2 | 3 | Client = require('rockets'); 4 | Demo = require('./lib/Demo.js'); 5 | 6 | new Demo().run(); 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rockets-demo", 3 | "version": "1.0.3", 4 | "description": "Demo for 'rockets'", 5 | "license": "MIT", 6 | "keywords": [ 7 | "rockets", 8 | "reddit", 9 | "demo" 10 | ], 11 | "author": { 12 | "name": "Rudi Theunissen", 13 | "email": "rudolf.theunissen@gmail.com", 14 | "url": "https://github.com/rtheunissen" 15 | }, 16 | "dependencies": { 17 | "rockets": "^1.1.0", 18 | "colors": "^1.1.2" 19 | }, 20 | "devDependencies": { 21 | "coffee-script": "^1.9.3" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/rtheunissen/rockets-demo" 26 | }, 27 | "bugs": { 28 | "url": "https://github.com/rtheunissen/rockets-demo/issues" 29 | }, 30 | "scripts": { 31 | "start": "node index.js", 32 | "prepublish": "coffee -o lib/ -c src/" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Demo.coffee: -------------------------------------------------------------------------------- 1 | module.exports = class Demo 2 | 3 | constructor: () -> 4 | @client = new Client() 5 | 6 | 7 | run: () -> 8 | 9 | # Subscripe to posts and comments when connected. 10 | @client.on 'connect', => 11 | @client.subscribe 'posts' 12 | @client.subscribe 'comments' 13 | 14 | # Attempt to reconnect when the connection is dropped. 15 | @client.on 'disconnect', => 16 | @client.reconnect() 17 | 18 | # Print a comment when it is received. 19 | @client.on 'comment', (comment) => 20 | @print comment 21 | 22 | # Print a post when it is received. 23 | @client.on 'post', (post) => 24 | @print post 25 | 26 | # Attempt to reconnect when the connection is dropped. 27 | @client.on 'error', (err) -> 28 | console.error err 29 | 30 | @client.connect() 31 | 32 | 33 | # Pads a string to a target length with whitespace to the right. 34 | rpad: (string, targetLength) -> 35 | if string.length <= targetLength 36 | for _ in [0...targetLength - string.length] 37 | string += ' ' 38 | 39 | return string 40 | 41 | 42 | # Pads a string to a target length with whitespace to the left. 43 | lpad: (string, targetLength) -> 44 | if string.length <= targetLength 45 | for _ in [0...targetLength - string.length] 46 | string = ' ' + string 47 | 48 | return string 49 | 50 | 51 | # Prints a model to the console. 52 | print: (model) -> 53 | data = model.data 54 | 55 | # Local time of day when the model was created 56 | time = new Date(data.created_utc * 1000).toTimeString().split(' ')[0] 57 | 58 | if model.kind is 't1' 59 | type = @rpad('Comment', 7) 60 | text = data.body.replace(/\s/g, ' ') 61 | subr = data.subreddit 62 | user = data.author 63 | 64 | else 65 | type = @rpad('Post', 7) 66 | text = data.title 67 | user = data.author 68 | subr = if data.subreddit then data.subreddit else '(promoted)' 69 | 70 | length = @lpad("#{text.length}", 5) 71 | 72 | user = 'u/' + @rpad(user, 21).blue.bold 73 | subr = 'r/' + @rpad(subr, 22).red.bold 74 | 75 | console.log "[#{time}] #{type} by #{user} in #{subr} #{length}".grey 76 | --------------------------------------------------------------------------------