Node Js Websocket Js Meetup Slides

download Node Js Websocket Js Meetup Slides

If you can't read please download the document

Transcript of Node Js Websocket Js Meetup Slides

node.js & WebSocket

node.js & WebSocket

London Javascript meetup Jan 2010Makoto Inoue

Topics

Why WebSocket & node.js ?

What is WebSocket(s) ?

How do I use it ?

How does it work?

1.Why WebSocket & node.js

node.js is great ....

for chat app only ?

Activity monitor

http://bit.ly/52lSGy

Server & Client communication

* Ajax periodic pull is resource intensive .* Long polling is same as Ajax for server streaming

2. What is Websocket(s) ?

HTML5 standard

Implemented in Chrome

web-socket-js for flash fallback

Less network overhead

Cross domain

3. How do I use it (client side)?

there are also send() and close() functions

3. How do I use (server side)?

http://github.com/Guille/node.websocket.js/

node runserver.js

* the one I used for my activity monitor seems removed from git repo (I still have his fork)

* you can specify different handler under module dir which correspond with

modules/echo.js

3. How do I use (server side)?

http://github.com/ncr/node.ws.js

http://nodejs.org/api.html#_tcp

* correspond with node.js syntax* chained

* seems you can only add one handler

4.How does it work?

http://github.com/ncr/node.ws.jsnode.websocket.js => 200 linesnode.js => 100 lines

Step 1. Create TCP server

* HTTP is one layer above TCP server.

Step 2. Do handshake for first time

http://nodejs.org/api.html

Step 3. Handshake req/res

headerExpression is what client 's HTTP request.

handshakeTemplate is server's HTTP response.

Step 4. if ok, handle()

For the second request, since client and server did already handshake, it just sends message back and forth using tcp socket (hence using TCP server, not HTTP server)

The word "Upgrade" sounds like switching to completely different protocol, but the truth is it's downgrading to lower protocol.

Summary

WebSocket = Simple API

Node.js = Easy to write TCP server

WebSocket+Node.js+ (STOMP,XMPP,AMQP)?

01/25/10