Outline
              
            
        
        Node.js
是什麼:
- build scalable network applications using JavaScript on the server-side.
 - 據 JavaScript Runtime 寫成,因為大部份都是 C code ,所以很快。
 
可以做什麼:
- Websocket Server (像聊天室)
 - Fast File Upload Client
 - Any Real-Time Data Apps
 
不是什麼:
- A Web Framework
 - Multi-threaded(可視為 single threaded server )
 
Blocking Code vs Non-Blocking Code(callback)
- Blocking
 
一般為了做:
- Calls out to web services
 - Reads/Writes on the Database
 - Calls to extensions
 
  | 
  | 
- Non-Blocking(callback)
 
  | 
  | 
語法
  | 
  | 
- Same as
 
  | 
  | 
Event
將 Event 附加在 DOM 上。
Websocket 101
What
- 因為 bidirectional communication 的需求。
 - 如果兩邊終端(endpoints)非瀏覽器則不一定需要使用 Websocket 這個 protocol。
 - work with existing HTTP infrastructure,所以受到很多限制。
 - 不同於單純的 raw TCP connection,因為命名的關係常常聯想成傳統的 socket。事實上他在於訊息的傳輸是根據 UDP,但卻有 TCP 的 reliable,
 
websocket adds on top of that:
- upgrades from HTTP 1.1
 - Through the HTTP handshake
 
data structure
為了要存放 non-blocking message,使用 Redis。
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes