Skip to content

Commit eefdf1c

Browse files
author
Daniel Hendricks
committed
Moved configuration to wiki, added extra comments in demo JS
1 parent 659caeb commit eefdf1c

File tree

4 files changed

+35
-42
lines changed

4 files changed

+35
-42
lines changed

README.md

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@
99
A simple example of using [Node.js](https://nodejs.org/) to send Growl-style messages to a browser via [Socket.IO](https://socket.io/).
1010

1111
- [Installation](#installation)
12-
- [Configuration](#configuration)
12+
- [Configuration](https://github.com/dmhendricks/nodejs-simple-message-relay/wiki/Configuration)
13+
- [Setting Environment](https://github.com/dmhendricks/nodejs-simple-message-relay/wiki/Configuration#setting-environment)
1314
- [Usage](#usage)
1415
- [Screenshot](#screenshot)
1516

1617
### Requirements
1718

1819
- [Node.js](https://nodejs.org/)
1920

21+
### How It Works
22+
23+
**The server does one thing:** It accepts JSON data submitted to the endpoint and forwards it to the specified socket. It will relay _any_ data that you submit - it is up to the listening client to consume it.
24+
25+
You can have any number of clients listening to different sockets. In this way, you can have multiple apps listening for different events and different payloads. Purely as examples, you could have all of the following:
26+
27+
- An admin panel that listens on a socket named `admin-notifications`, receiving information about system events.
28+
- An e-commerce site that listens on a socket named `sales-popups`, which displays a sale notification to visitors, similar to [Sales Pop](http://sales-pop.demo.beeketing.com/).
29+
- A real-time chat application that listens on a socket named `chat-app`, which broadcasts user messages as they are submitted.
30+
31+
These are just examples - you can relay any data to any number of sockets that you wish. **The included demo page is simply an example of consuming data received from the server.** Your usage will vary.
32+
2033
### Goals
2134

2235
- [x] Add ability to limit specific socket names
@@ -26,7 +39,7 @@ A simple example of using [Node.js](https://nodejs.org/) to send Growl-style mes
2639
- [ ] Add example with custom template and image
2740
- [ ] Improve input validation
2841
- [ ] Improve exception handling
29-
- [ ] WordPress plugin to function similar to [Sales Pop](http://sales-pop.demo.beeketing.com/)
42+
- [ ] WordPress plugin and integrations
3043

3144
## Installation
3245

@@ -40,37 +53,6 @@ npm run start
4053
```
4154
:pushpin: For local development, you can use `npm run dev` instead to automatically reload the server as you modify and save files.
4255

43-
## Configuration
44-
45-
Configuration files are located in the `config` directory. `default.json` is used when `NODE_ENV` is not defined or where a matching environment configuration file does not exist.
46-
47-
### Setting Environment
48-
49-
If you'd like to have a different [configuration file](https://github.com/lorenwest/node-config/wiki/Configuration-Files#file-load-order/) a particular environment ("production", for example):
50-
51-
1. Set the environment at the command line: `export NODE_ENV=production`
52-
2. Create a `config/production.json` file with variables from [`config/default.json`](https://github.com/dmhendricks/nodejs-simple-message-relay/blob/master/config/default.json) that you'd like to override. Example:
53-
54-
```json
55-
{
56-
"server": {
57-
"address": "0.0.0.0"
58-
},
59-
"demo_page": false,
60-
"api_keys": [
61-
"abc123def",
62-
"tuv456xyz"
63-
],
64-
"sockets": [
65-
"my-socket-name"
66-
]
67-
}
68-
```
69-
70-
:warning: If `api_keys` is an empty array, validation will not occur and **_all message_** will be accepted (useful for local development and debugging, not recommended when publicly accessible).
71-
72-
:pushpin: See [restify-cors-middleware](https://github.com/Tabcorp/restify-cors-middleware#usage) for CORS configuration options.
73-
7456
## Usage
7557

7658
Once the server is running:

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Gulpfile for Node.js Simple Message Relay
33
* Usage: npx gulp
4+
* npx gulp [task_name]
45
*/
56

67
let pkg = require( './package.json' ),
@@ -32,7 +33,7 @@ const BROWSERS_LIST = [ 'last 2 version', '> 1%', 'ie >= 11', 'last 2 Android ve
3233
* Gulp Tasks
3334
*/
3435

35-
// Watch files
36+
// Watch files for changes
3637
function watchFiles() {
3738
gulp.watch( `${src.js}/**/*.js`, gulp.series( 'taskJS' ) );
3839
gulp.watch( `${src.sass}/**/*.scss`, gulp.series( 'taskCSS' ) );

public/assets/js/main.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
55

66
(function($) {
77

8-
var socket_name = 'my-socket-name';
8+
var socket_name = 'my-socket-name'; // Name as you wish
99
var status = $( '#status' ), submit_button = $( 'button.submit' ), simple_notification = $( '#simple_notification' ), simple_notification_color = $( '#simple_notification_color' );
1010
var notify = $.noist( { position: 'bottom left' } );
1111
notify.options.duration = 1500;
1212

13-
// Set connection state
13+
// Display connection state
1414
socket.on( 'connect', function() {
1515

1616
status.attr( 'data-connected', true ).html( 'Connected' );
@@ -32,7 +32,8 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
3232

3333
console.log( `Recevied [${socket_name}]`, response );
3434

35-
// Display notification
35+
// Display notification - For this example, I am displaying a slide-out message on the demo page.
36+
// You can consume the response data as you desire and for your own needs.
3637
notify.message( response.message, response.color );
3738

3839
});
@@ -41,13 +42,15 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
4142
submit_button.on( 'click', function( event ) {
4243

4344
event.preventDefault();
45+
46+
// Disallow submitting empty messages
4447
if( !$( 'input' ).val().trim() ) {
4548
$( 'input' ).addClass( 'empty' );
4649
return;
4750
}
4851
simple_notification.removeClass( 'empty' );
4952

50-
// Send message to Socket.IO
53+
// Send message to Socket.IO endpoint
5154
jQuery.ajax ({
5255
url: url + '/send/' + socket_name,
5356
type: 'POST',
@@ -58,8 +61,10 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
5861
dataType: "json",
5962
contentType: "application/json; charset=utf-8",
6063
success: function( response ){
64+
6165
simple_notification.val( '' );
6266
console.log( 'Message sent: ', response );
67+
6368
}
6469
});
6570

public/src/js/main.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
55

66
(function($) {
77

8-
var socket_name = 'my-socket-name';
8+
var socket_name = 'my-socket-name'; // Name as you wish
99
var status = $( '#status' ), submit_button = $( 'button.submit' ), simple_notification = $( '#simple_notification' ), simple_notification_color = $( '#simple_notification_color' );
1010
var notify = $.noist( { position: 'bottom left' } );
1111
notify.options.duration = 1500;
1212

13-
// Set connection state
13+
// Display connection state
1414
socket.on( 'connect', function() {
1515

1616
status.attr( 'data-connected', true ).html( 'Connected' );
@@ -32,7 +32,8 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
3232

3333
console.log( `Recevied [${socket_name}]`, response );
3434

35-
// Display notification
35+
// Display notification - For this example, I am displaying a slide-out message on the demo page.
36+
// You can consume the response data as you desire and for your own needs.
3637
notify.message( response.message, response.color );
3738

3839
});
@@ -41,13 +42,15 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
4142
submit_button.on( 'click', function( event ) {
4243

4344
event.preventDefault();
45+
46+
// Disallow submitting empty messages
4447
if( !$( 'input' ).val().trim() ) {
4548
$( 'input' ).addClass( 'empty' );
4649
return;
4750
}
4851
simple_notification.removeClass( 'empty' );
4952

50-
// Send message to Socket.IO
53+
// Send message to Socket.IO endpoint
5154
jQuery.ajax ({
5255
url: url + '/send/' + socket_name,
5356
type: 'POST',
@@ -58,8 +61,10 @@ const socket = io.connect( url, { reconnection: true } ); // Set reconnection to
5861
dataType: "json",
5962
contentType: "application/json; charset=utf-8",
6063
success: function( response ){
64+
6165
simple_notification.val( '' );
6266
console.log( 'Message sent: ', response );
67+
6368
}
6469
});
6570

0 commit comments

Comments
 (0)