/nodejs/3.1/examples/node-to-browser/node-app.js

https://github.com/apalsapure/pubnub-api · JavaScript · 34 lines · 18 code · 4 blank · 12 comment · 0 complexity · 29a6768105ff3a61dab56c39ebb7512b MD5 · raw file

  1. /* ---------------------------------------------------------------------------
  2. Init PubNub and Get your PubNub API Keys:
  3. http://www.pubnub.com/account#api-keys
  4. --------------------------------------------------------------------------- */
  5. console.log('Broadcasting Messages from Node...');
  6. require('child_process').exec('open index.html');
  7. var pubnub = require("./../../pubnub.js").init({
  8. publish_key : "demo",
  9. subscribe_key : "demo"
  10. });
  11. /* ---------------------------------------------------------------------------
  12. Listen for Messages
  13. --------------------------------------------------------------------------- */
  14. pubnub.subscribe({
  15. channel : "my_node_channel",
  16. callback : function(message) {
  17. console.log( "Message From Browser - ", message );
  18. }
  19. });
  20. /* ---------------------------------------------------------------------------
  21. Type Console Message
  22. --------------------------------------------------------------------------- */
  23. setInterval( function() {
  24. pubnub.publish({
  25. channel : "my_browser_channel",
  26. message : 'Hello from Node!'
  27. });
  28. }, 1000 );