Difference between socket and websocket?
Asked Answered
T

6

287

I'm building web app that needs to communicate with another application using socket connections. This is new territory for me, so want to be sure that sockets are different than websockets. It seems like they're only conceptually similar.

Asking because initially I'd planned on using Django as the foundation for my project, but in the SO post I linked to above it's made very clear that websockets aren't possible (or at least not reliable, even with something like django-websockets) using the preferred Django setup (Apache with mod_wsgi). Yet I've found other posts that casually import Python's socket module for something as simple as grabbing the server's hostname.

So:

  • Are they really different?
  • Is there any reason not to use Django for a project that relies on establishing socket connections with an outside server?
Toner answered 11/2, 2011 at 20:19 Comment(1)
My understanding of a WebSocket is: a protocol for inter-socket connections that Javascript on a web application can establish.Bang
C
256

To answer your questions.

  1. Even though they achieve (in general) similar things, yes, they are really different. WebSockets typically run from browsers connecting to Application Server over a protocol similar to HTTP that runs over TCP/IP. So they are primarily for Web Applications that require a permanent connection to its server. On the other hand, plain sockets are more powerful and generic. They run over TCP/IP but they are not restricted to browsers or HTTP protocol. They could be used to implement any kind of communication.
  2. No. There is no reason.
Catbird answered 11/2, 2011 at 20:27 Comment(12)
WebSockets are not similar to HTTP. They are normal sockets with some framing and an HTTP-compatible handshake. The HTTP-compatible handshake is just to allow WebSocket connection on the same port that a webserver is running on (so the webserver can forward them), but once the connection is established, the webserver is not in the loop. WebSockets is not limited to browser clients. See libwebsocket which has both a non-browser client and a server.Petrel
OK. I see... Since I can't delete this accepted answer, I would kindly ask you to edit it with correct information. Thanks!Catbird
You can build websocket clients and use them outside of browsers. The websocket protocol is HTTP 1.1 with an upgraded connection to "websocket."Hautboy
@PabloSantaCruz : can you please elaborate "So they are primarily for Web Applications that require a permanent connection to its server" ? I am asking this because in case of sockets also we do need a permanent connection to the server. So what is the difference basically?Kommunarsk
@Petrel After the HTTP-compatible handshake and an upgrade request, is it then just the same as a raw TCP connection?Metaphysical
@Metaphysical nope. There is a small bit of framing: tools.ietf.org/html/rfc6455#section-5 (2 bytes for small messages).Petrel
@Petrel does that framing add a large enough overhead to stop development of triple a mmo's? (assuming it's browser created) -- or should they be fine?Scandinavian
@NiCkNewman that's probably a good separate question actually. The framing overhead is minimal and is not the problem. The problem with WebSockets for MMO networking is two-fold: it is client server only, and it is TCP (stream based). For good MMO networking performance you also need direct peer-to-peer networking and you need datagram (consistent low latency is more important than receiving every packet). The good news is that WebRTC DataChannel has both of these properties so it will begin to fill the gap and allow full AAA MMO gaming in the browser.Petrel
@Petrel I was looking at the console for the discord app, and they utilize the new WebRTC stuff immensely. I am glad that the framing overhead is not an issue, I was getting worried. Thanks for the quick answer :)Scandinavian
Sockets are not restricted to TCP only. They are nothing more than an abstraction of an endpoint, the protocol, and a port. Can be TCP, UDP, raw IP, or any custom protocol.Sporulate
@Petrel your comment is the correct answer, whereas the original answer is misleading and should be down vited.Hebbel
I think plain sockets can also use UDP and are not limited to TCP.Tillio
H
35

Websockets use sockets in their implementation. Websockets are based on a standard protocol (now in final call, but not yet final) that defines a connection "handshake" and message "frame." The two sides go through the handshake procedure to mutually accept a connection and then use the standard message format ("frame") to pass messages back and forth.

I'm developing a framework that will allow you to communicate directly machine to machine with installed software. It might suit your purpose. You can follow my blog if you wish: http://highlevellogic.blogspot.com/2011/09/websocket-server-demonstration_26.html

Hautboy answered 29/9, 2011 at 17:1 Comment(0)
C
20

WebSocket is just another application level protocol over TCP protocol, just like HTTP.

Some snippets < Spring in Action 4> quoted below, hope it can help you understand WebSocket better.

In its simplest form, a WebSocket is just a communication channel between two applications (not necessarily a browser is involved)...WebSocket communication can be used between any kinds of applications, but the most common use of WebSocket is to facilitate communication between a server application and a browser-based application.

Capitalistic answered 10/11, 2015 at 12:13 Comment(0)
F
17

WebSocket is a computer communications transport protocol (like TCP, HTTP 1.0, HTTP 1.1, HTTP 2.0, QUIC, WebRTC, etc.)

Socket is an endpoint for sending and receiving data across the network (like Port number)

Example of Socket:

(TCP, 8.8.8.4, 8080, 8.8.8.8, 8070)

where:

(protocol, local address, local port, remote address, remote port)
Fubsy answered 3/6, 2021 at 17:53 Comment(1)
I think this answer clears it up for me. WebSocket is a protocol, Socket is an endpoint - got it thanks. Also, a simple google search returned "The current API specification allowing web applications to use this protocol(WebSocket) is known as WebSockets" (note the plural).Benefice
S
14

You'd have to use WebSockets (or some similar protocol module e.g. as supported by the Flash plugin) because a normal browser application simply can't open a pure TCP socket.

The Socket.IO module available for node.js can help a lot, but note that it is not a pure WebSocket module in its own right.

It's actually a more generic communications module that can run on top of various other network protocols, including WebSockets, and Flash sockets.

Hence if you want to use Socket.IO on the server end you must also use their client code and objects. You can't easily make raw WebSocket connections to a socket.io server as you'd have to emulate their message protocol.

Selfinductance answered 11/2, 2011 at 20:45 Comment(3)
Why can't a normal browser application simply open a pure TCP socket, just curious? Is this because javascript doesn't support it?Woodbury
@AdamHughes yup, pretty much. I imagine most security folks would be up in arms at the idea of web browsers opening up arbitrary TCP connections to strange places all over the internet.Selfinductance
@AdamHughes that would allow any random web page to make your computer connect to anything and do anything. For example some random web page might cause your computer to download child porn in a way that looks like you did it. The extra overhead in websockets makes sure the server knows it's not talking to the user, but some web page the user has opened.Huambo
J
5

Regarding your question (b), be aware that the Websocket specification hasn't been finalised. According to the W3C:

Implementors should be aware that this specification is not stable.

Personally I regard Websockets to be waaay too bleeding edge to use at present. Though I'll probably find them useful in a year or so.

Jassy answered 11/2, 2011 at 22:59 Comment(2)
What about now, 9 years later?Hollandia
Pretty sure they're commonly used now. Many websites have real-time or live-updating features powered by websockets, although various client libraries offer "fallback to polling" functionality in case server support is missing.Moschatel

© 2022 - 2024 — McMap. All rights reserved.