WebSocket vs Sockets
Hello, sorry to disturb you all, I have a small question. I was reading about WebSockets and Sockets in general. My question is, is
WebSocket
a "pure" socket in the sense that it's just an endpoint where data can be fetched or sent?
For instance, I read that WebSocket
is just a protocol that gets updated whenever an HTTP request is sent using the upgrade header
and that WebSocket
lies on the application layer on the OSI model rather than the transport layer which includes TCP/UDP sockets.
I'm a bit confused of how can "WebSockets", being "sockets" lies on the application layer. Would really appreciate if someone can clarify that please.21 Replies
You're really wanting to get into the weeds here, huh? lol
Ok, so
WebSockets
is nothing more than a protocol on top of HTTP. Which is, itself, a protocol on top if TCP/IP.
While a socket is simply a way for two computers (or two programs on the same computer) to exchange data.
WebSockets are a protocol used for bi-directional data transfer that communcates via a computer's socket.yeah, I see, so "WebSocket" is just a protocol, it's not a socket in itself
Correct
a socket can be anything, as long and you can have 2 things communicate on it
under the hood it make use of sockets but by itself it's just a set of rules (protocol)
it uses a socket
it works like a socket
but it is the data that goes in the socket
yeah in an abstract way
no, no, it uses a tcp socket, to communicate
if im not mistaken
let me double check
Yes, WS uses TCP as it's an HTTP-based protocol and HTTP uses TCP
yes
yep I see
yeah I think that was my confusion 😂, thanks !!
it is weird, i know
but since websockets are a protocol, you can use websockets over ipoac
you can even send letters with the data in it
it's just the data and how stuff works at that layer
it describes the http communication needed to initiate the protocol and blah blah blah
yep I see
WS is a pretty fun protocol to read, but implementation can be…difficult. What with the different frames, bit masking, and the like
Especially when trying to do that in JS. JS isn't the best at CPU bound tasks. So write it in C and compile to WASM lol
it is a miracle that it works
Was also reading that instead of using websockets, there is also something called "gRPC" but that's a topic for another day 😂
RPC is remote procedure call. A fancy way of saying "call a function on another computer". You need to know the function to be called as well as the function signature. It's just a protocol to allow for remote access of functions.
If you were using a WS, you'd need to send a specific message over the WS connection that would then be read by the target computer and then the target computer would call the function. RPC bypasses the intermediate layer of the WS (or other more generic connection) and just says, "hey, target machine, call
[function X]
with [params]
, kthxbai"ah ok, with web socket, we just send a message then the server need to interpret it and figure out with function to call, but with RPC, we already know which function to call, we just pass in the necessary inputs to form the function signature and that function get executed on the remote machine and we have a response object back?
RPC is an abstraction layer around "just call the damn function, Shinji"
Yep I see
it's a fancy jsonp
less janky