- bi-directional
- before you had to poll the server
- or use webhooks
- now the server can push to you
- mainly used in browsers
- keeps a constant connection open
Android gif
WebSockets are Sockets, just with extra protocols:
- Connection scheme
- Security scheme
- Steps to follow to connect
WebSockets start with an HTTP request from client to server. Then the connection "upgrades" to the WebSocket protocol.
- WebRTC is peer to peer, not client/server
- WebRTC is better suited for audio/video
- WebSockets are better for text or transferring of bytes
- Connecting: the initial state of each web socket. Messages may be enqueued but they won't be transmitted until the web socket is open.
- Open: the web socket has been accepted by the remote peer and is fully operational. Messages in either direction are enqueued for immediate transmission.
- Closing: one of the peers on the web socket has initiated a graceful shutdown. The web socket will continue to transmit already-enqueued messages but will refuse to enqueue new ones.
- Closed: the web socket has transmitted all of its messages and has received all messages from the peer.
- Canceled: the web socket connection failed. Messages that were successfully enqueued by either peer may not have been transmitted to the other.
State progression is independent for each peer.
Arriving at a gracefully-closed state indicates that a peer has sent all of its outgoing messages and received all of its incoming messages. But it does not guarantee that the other peer will successfully receive all of its incoming messages.
Just cuz you closed gracefully, doesn't mean the other side did.
- Looks like you can use builder pattern or constructors
gif/video of the grinch echo https://www.youtube.com/watch?v=ZU0wyJxAZ6c
#Live code?
See this list: http://stackoverflow.com/questions/30547517/which-websocket-library-to-use-in-android-app
- Not supported by HTTP/2
- Can drain battery if connection is constantly open
- Could be negatively affected by Doze
- WebRTC
- WTF is a streaming API? It just keeps the connection alive. Seems dumb to me.