Skip to content

Commit

Permalink
first simple Pusher Pipe node.js example
Browse files Browse the repository at this point in the history
  • Loading branch information
leggetter committed Jan 7, 2012
1 parent 64a73c0 commit 562db91
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.DS_Store
php/config.php
ruby/config.rb
pusher_pipe/node/config.js
pusher_pipe/node/node_modules

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ You can view the code in action here:

Shows how to exclude recipients from receiving events when trigger events using the Pusher Ruby gem.

### QuickStart example for the Pusher Pipe (Node.js)

`pusher_pipe/node/src`

## Want to see an example?

Please email [support@pusher.com](mailto:support@pusher.com?subject=Pusher Example request).
Expand Down
29 changes: 29 additions & 0 deletions pusher_pipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#Simple Pusher Pipe Example

For more information on the Pusher Pipe see:
<http://pusher.com/docs/pipe>

## Technologies

The Pusher Pipe only has a client library for Node.js at the moment.

## Example

### Node.js

To run this example you must install the `pusher-pipe` node.js module. You can find the module here:
http://search.npmjs.org/#/pusher-pipe

And install using:

npm pusher-pipe

See the [Pusher Pipe QuickStart](http://pusher.com/docs/pipe_quickstart) for more information.

The node example found in `node/src/` is based on the [Pusher Pipe QuickStart](http://pusher.com/docs/pipe_quickstart). It demonstrates how to:

* Be notified of a socket connection on the server
* trigger an event from the client on the `back_channel`
* trigger an event from the server on the `back_channel`
* subscribe to a channel and bind to an event on the client
* trigger an event on a channel from the server
5 changes: 5 additions & 0 deletions pusher_pipe/node/config.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var PUSHER_CONFIG = {
APP_ID: 'YOUR_APP_ID',
APP_KEY: 'YOUR_APP_KEY',
APP_SECRET: 'YOUR_APP_KEY'
};
48 changes: 48 additions & 0 deletions pusher_pipe/node/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<html>
<head>
<script type="text/javascript" src="http://js.pusherapp.com/1.10.0-pre/pusher.min.js"></script>
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script type="text/javascript">
Pusher.host = "ws.darling.pusher.com"
Pusher.log = function(message) {
log(message);
};
var pusher = null;

$(init);

function init() {
$("#send").click(sendEvent);

createPusher();
};

function createPusher() {
pusher = new Pusher('577f0d13ea03ad86c8d5');

var channel = pusher.subscribe('my-channel');
channel.bind('my_event', function(data) {
log( JSON.stringify(data) );
});
}

function sendEvent() {
log("sending");
pusher.back_channel.trigger('eventFromBrowser', {some: 'data'});
pusher.back_channel.bind('acknowledge', function(data) {
log('back_channel: ' + JSON.stringify(data));
});
}

function log(msg) {
$('#logs').prepend('<div>' + msg + '</div>');
};
</script>
</head>
<body>
<button id="send">Send Event</button>

<h3>Logs</h3>
<div id="logs"></div>
</body>
</html>
30 changes: 30 additions & 0 deletions pusher_pipe/node/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var config = require('./config.js');
console.log(config);

var sys = require('sys');
var Pipe = require('pusher-pipe');

var client = Pipe.createClient({
key: config.PUSHER_APP_KEY,
secret: config.PUSHER_APP_SECRET,
app_id: config.PUSHER_APP_ID,
debug: true
});

client.subscribe(['socket_message', 'socket_existence']);

client.on('connected', function() {
console.log('>>>>>>> connected event');
});

client.sockets.on('open', function(socketId) {
console.log('>>>>>>> Connection by ' + socketId);
});

client.connect();

client.sockets.on('event:eventFromBrowser', function(socket_id, data){
client.socket(socket_id).trigger('acknowledge', {message: 'back channel event'});

client.channel('my-channel').trigger('my_event', {message: 'standard channel event'});
})

0 comments on commit 562db91

Please sign in to comment.