GeekCampSG - Nodejs , Websockets and Realtime Web

1,528 views

Published on

Talks about how Node.js and Socket.io are changing the way how Realtime apps are being developed today .

Published in: Technology, Education
2 Comments
0 Likes
Statistics
Notes
  • I agree. But sometimes people want more control .
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice presentation, just want to add one thing, sometimes instead of building real-time applications overselves using socket.io, node.js, etc., we can use hosted services like http://pusher.com/. It's easier to integrated stuff, than to build things and maintain infrastructure, based on my past experience. However, it varies from case to case.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
1,528
On SlideShare
0
From Embeds
0
Number of Embeds
217
Actions
Shares
0
Downloads
0
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

GeekCampSG - Nodejs , Websockets and Realtime Web

  1. 1. Node.js ,Socket.io andthe Magic Bhagaban Behera
  2. 2. What is Node.js ?Created by Ryan Dahl ( Joyent )Command line tool to run JavascriptBuilt on Top of Google’s V8 Javascript EngineProvides Javascript API for Network and File System . Used tocreate Servers, Networking tools.Event-driven , non-blocking I/O APIsCore written in C/C++ . Supports C/C++ addons.Can handle thousands of concurrent connections with minimaloverhead on a single process.
  3. 3. What’s the need ?
  4. 4. Node.js - FeaturesReally Lean( 8000 lines of C/C++and 2000 lines of JS )Exceptionally FastEasily Scalable
  5. 5. Event-Driven ApproachAnalogy – Bank ReceptionAnalogy - Restaurant
  6. 6. The Cost of IOAccessing RAM ~250 CPU cyclesDisk Operations ~41 000 000 CPU cycles Reading a file Writing to a fileNetwork IO ~240 000 000 CPU cycles Database Query HTTP Responses
  7. 7. Handling I/O Apache is multithreaded (depending on conf.) Spawns a thread per request (or process) Each request is in its own "box” Blocking IOresult = query(SELECT * FROM ...);print result.id;
  8. 8. The Node waySingle thread for your code...but, I/O runs in parallelHandle thousands of concurrent connectionswith a single processNeed to be very careful with CPU-intensivecode
  9. 9. Why JavaScript ?• Single Threaded , Asynchronous• Non blocking IO• Ubiquitous• Massive codebase of libraries• One language to rule them all( Full stack )
  10. 10. Hello World !HellowWorld.js HellowWorld.phpsetTimeout(function(){ echo(“Hello”);console.log(“World”); sleep(2000);},2000); echo(“World”);console.log(“Hello”);
  11. 11. Our own HTTP Servervar http= require(http);var s = http.createServer(function(req,res){res.writeHead(200,{content-type : text/plain}) ; res.end("hello world n");})s.listen(8000);
  12. 12. Built-in Modules• File System require(fs);• HTTP Client and Server require(http);• TCP Sockets require(net);• Many more: http://nodejs.org/docs/v0.4.8/api/
  13. 13. Express makes HTTP easier• Nodes HTTP module is low level.• Express is a simple framework inspired by Sinatra (Ruby).• InstallingExpress: npm install express• Robust verb-based routing app.get(/about, function(req, res) { });• Templating(with Jade or EJS)• Sessions
  14. 14. When to use Node.js ?Realtime AppsCrawlersSingle-page AppsStreamingFile uploadingOnline GamingCollaborationLive SportsLive chatFinancial Applications
  15. 15. Getting StartedNode.js: http://nodejs.org/#downloadBuildinstructions: http://bit.ly/egLfzu Mac:brew update && brew install nodenpm, a package manager for Node:curl http://npmjs.org/install.sh | shMany modules at: http://search.npmjs.org/Windows support isnt great.
  16. 16. Node PAASNodeJitsuNodesterJoyentHeroku
  17. 17. Socket.io
  18. 18. Real-time Web
  19. 19. HistoryStatic AppsDynamic AppsAjax AppsReal-time Apps
  20. 20. HTTP
  21. 21. Ajax Polling
  22. 22. Long Polling
  23. 23. Sockets
  24. 24. Websockets / Socket.ioSocket.IO aims to make realtime apps possible in everybrowser and mobile device, blurring the differences between thedifferent transport mechanisms. Its care-free realtime 100% inJavaScript.
  25. 25. Show me the codeVisit http://socket.io/#how-to-use for excellent andsimple documentation .
  26. 26. Contact me !bhagaban@noisestreet.com / @bhoga

×