# Websockets
- Web IDL
- Web IDL is an interface description language (IDL) format for describing application programming interfaces (APIs) that are intended to be implemented in web browsers.
- XHR - Xml HTTP Request
- is an API, whose objects are provided by the browser's JavaScript environment.
# How to get realtime updates from server?
- Short Polling
- Long Polling - poll and then wait for sometime, until server sends response
- Websockets - full duplex communication
Send HTTP GET request via telnet
telnet www.google.com 80
GET /
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
# Websockets
HTTP is unidirectional, stateless
**ws**://www.google.com
**wss**://www.google.com
devtools inspect ws
Chrome -> devtools -> Netowrk tab -> WS
- providing full-duplex communication channels over a single TCP connection
- being standardize by W3C in Web IDL
- works at Layer 7 in OSI model
- 2 parts
- Handshake to establish connection
- Uses HTTP upgraer header
- then actual data transfer
- similar to network MAC Protocols, CSMA/CA
- Handshake to establish connection
- Nginx has supported WebSockets since 2013
- Masks data from other connections
WARNING
WebSocket servers must validate the "Origin" header against the expected origins during connection establishment, to avoid Cross-Site WebSocket Hijacking attacks
Works in 3 stages
- Opening Handshake
- Data Exchange
- Close Connection
# Where and when websockets can be used?
feeds are updated in realtime by websockets
- Gaming Applications
- Chat Applications
- Realtime online crypto exchanges
- Social feeds
- Collaborative editing/coding
- SPorts update
# Beginners Guide to Websockets
upgrade to HTTP
- How fast are they over traditional HTTP
- Transport Layer
- TCP, UDP
- Internet Layer
- IPv6, IPv4
- Application Layer
- HTTP, Websocket, SSL, IMAP, POP
- Meet AJAX
- How to implement it?
- Browser Support
- cannot communicate with REST
- namespaces
- fail backs
- Polling
- Long Polling, keep connection open until new data has arrived
- Server Sent Events
- Event source API
- not truly bi-directional
- need a event loop
- works well with existing APIs
# Websockets vs AJAX
# HTTP 2
- Decrease latency to improve page load speed in web browsers by considering:
- data compression of HTTP headers
- HTTP/2 Server Push
- pipelining of requests
- fixing the head-of-line blocking problem in HTTP 1.x
- multiplexing multiple requests over a single TCP connection
# How is it websocket different from HTTP?
# How does HTTP work?
- connection is opened and closed by server
# Make realtime application that updates data from server?
- Browserstack
- pro.coinbase
- financial exchanges
- realtime news/social feeds
- realtime chat
- uber - watch cars moving
# How to implement and use WS?
# .NET Framework
we recommend SignalR over raw WebSockets
- SignalR provides transport fallback for environments where WebSockets is not available
A variable which holds socket connection, initialized on document load
Sysnchronous vs Asynchronous principles
HTTP, XMLHTTPRequest and websockets concepts
Websocket API
- Methods
- send
- close
- Events
- open
- Message
- Error
- close
- Methods
# Socket Programming | Nettwork Programming
To build any networked application, we need socket Programming
- Basics of TCP/IP Protocol connection
- What and how do we program a connection?
- Client Server Model
- What are
requests
andresponse
? - Which program generates them?
# Berkley's Socket API
- What are [Socket]? to be specific Berkley Sockets
- Are GPC different from sockets?
- Yes, RPC are meant for OS whereas sockets are for communication b/w computers over the network
Good Questions
- How does traditional HTTP protocol works?
- How did AJAX changed it?
- What do websockets offer? and at what cost?