We have an application that connects to a server and sends it position every 15 seconds. Now our client has asked to "upgrade" our current TCP/IP connection into WebSocket. The reason for this is he heard it used less bandwidth and he wants to diminish the 15 seconds to 1 second. (not a public application so battery drain is not really a problem)
I have already done some research and many WebSockets vs HTTP comparisons but only 2 or 3 comparisons of WebSocket vs TCP/IP.
I've already found out that :
WebSockets are basically the same as TCP/IP but TCP/IP works on a lower layer than WebSockets.
WebSockets might use less bandwidth because its protocol might be more efficient than our current protocol.
WebSockets use more resources on the server side.
My question is: is it worth it to change our existing code and make use of WebSockets instead of the good ol' TCP/IP sockets?
As you appear to know already, a webSocket IS built on top of a TCP/IP connection. It's a specific protocol built on top of TCP.
My question is: is it worth it to change our existing code and make
use of WebSockets instead of the good ol' TCP/IP sockets?
If your code already works with a TCP socket and you don't need to interoperate with a browser client and you don't need any specific features built into webSockets, then there is no reason to rewrite perfectly functioning TCP socket code.
We have an application that connects to a server and sends it position
every 15 seconds. Now our client has asked to "upgrade" our current
TCP/IP connection into WebSocket. The reason for this is he heard it
used less bandwidth and he wants to diminish the 15 seconds to 1
second. (not a public application so battery drain is not really a
problem)
Whether or not a webSocket would be more bandwidth efficient than your existing TCP connection depends entirely upon what protocol you're running now on the TCP connection. If your existing protocol is very inefficient, then you would benefit from using a more efficient protocol whether that be webSocket or something else. I'd be surprised if switching to a webSocket would reduce anything from 15 seconds to 1 second unless the implementation of what you have now is just really inefficient.
We could only really comment further on a comparison between the two protocols if we could see exactly how your existing code/protocol works. If what you have now is really bad, then switching to a professionally designed system like webSockets might help you, but there's nothing innate in webSockets that makes them more efficient than some other well designed protocol.
Some of the reasons to use a webSocket over a plain TCP connection with some other protocol on it are as follows:
When you want a browser to be able to connect to you (browsers support plain http requests and webSocket connections - that's it).
When you specifically want the message passing type of paradigm that a webSocket offers and you don't already have a specific protocol to use over TCP (in other words when webSocket saves you from having to implement your own protocol).
When you want to connect to some other type of client that can more easily and quickly use a webSocket connection than it can some other protocol or your custom protocol.
When you're trying to share a port with a web server. By their nature of being initiated via an HTTP connection that is then switched over to the webSocket protocol, webSockets can operate on the same port as an HTTP server (sharing it with the web server).
When you want the interoperability with proxies and other network infrastructure that http and webSockets provide.
Related
I understand that WebSockets are built on TCP sockets and provide more user-friendly interfaces in terms of messages completion (see for instance this SO question: Differences between TCP sockets and web sockets, one more time ).
But could this alone justify choosing websockets over TCP sockets in a context where no web browser involved? Would such a design choice make sense or should WebSocket use be restricted to 'Web' environnents only?
The main point of WebSockets is that they can be integrated into the typical HTTP infrastructure, i.e. they start with a HTTP handshake (or HTTPS with wss://) and are also designed to deal with HTTP proxies, look (initially) like HTTP and can use the same ports so that they can pass through firewalls ...
If these properties are needed than WebSockets are a good choice even outside of browsers. If these are not needed less complex protocols might be sufficient instead.
WebSockets have an advantage over a custom TCP/IP solutions, since WebSockets experience less connectivity issues.
This is especially true for TLS + WebSockets (wss://) over port 443, and it's a benefit that isn't browser (or HTTP) specific.
This is because many firewalls, bridges and other intermediaries block different TCP/IP ports or allow limited traffic to pass through. Traffic over the HTTP and HTTPS ports (80 and 443) is often allowed even in stricter environments, which improves the chance of establishing (and retaining) a connection.
I want an http message to be sent and processed quickly by a remote server, via an already established persistent TCP connection
How can I optimize the communication?
I have a few ideas, but I am not knowledgeable enough about networking to know if they make sense:
HTTP sits on top of TCP. But how does it work exactly? Specifically, if I send 1 http message, does it translate into only 1 tcp message? (I know the initial handshake takes 3 round trip time, but I do not care about this, as the connection is already established). I guess it depends on the Maximum Segment Size that the server can accept?
Can I ask the server for a bigger maximum segment size if needed? How can I do it (I use python, httplib and socket modules, it would be ideal in this language).
The remote server works with TCP, but could I try sending it UDP messages? I know UDP is faster, but could this idea work?
I'll allow myself to comment/answer in-text:
I have a few ideas, but I am not knowledgeable enough about networking
to know if they make sense:
Exactly! Read up on TCP and HTTP, on wikipedia, for a starter. It will make things much easier to discuss. Probably also faster than asking on stackoverflow ;)
HTTP sits on top of TCP. But how does it work exactly?
Well, exactly like protocols work over each other in a layered protocol stack. Read Wikipedia's TCP article, and about the OSI/ISO layer model.
Specifically, if I send 1 http message, does it translate into only 1
tcp message?
No. HTTP itself doesn't care (and it doesn't have to) into how many lower level packets communication gets split.
(I know the initial handshake takes 3 round trip time,
but I do not care about this, as the connection is already
established).
3 time? That's not something that makes sense. Read about the TCP handshake and how HTTP asks for a document.
I guess it depends on the Maximum Segment Size that the
server can accept?
Among a lot of different other factors; really: HTTP doesn't care the least!
Can I ask the server for a bigger maximum segment size if needed?
No. Your network stack will most probably automatically use the biggest MTU that works.
How can I do it (I use python, httplib and socket modules, it would be
ideal in this language).
The remote server works with TCP, but could I try sending it UDP messages?
There are some specialized HTTP-over-UDP protocols, but they are not HTTP. Generally, HTTP is spoken over TCP, but again, the internet works on a layered protocol stack, and higher level protocols usually don't care what transports their data -- you could perfectly well have an HTTP session over carrier pidgeons!
I know UDP is faster, but could this idea work?
It's not. That's a misconception. UDP doesn't have automatic re-requesting for packets that got lost along the way, which, for things like multimedia or games might make sense, but using TCP gives you an ordered session, which is necessary for HTTP.
What is the difference between a TCP server/Net server in vertex and HTTP server?
What are the use cases for each?
I tried googling and went through the official website, none of them have a clear explanation.
First off, in General Networking, there are 2 common types of handling connections. This can be done either over TCP (Transmission Control Protocol) or UDP (User Datagram Protocol). The most import difference between these two is that UDP will continuously send out streams/buffers of bytes without checking to see if the network packets made it to the other side of the line. This is useful in situations where security isn't much of an issue and where speed is important. Most VoIP services (Skype, Hangouts), XMPP (chat) and even YouTube (I think) use UDP for their streaming, as it has huge gains on performances and it doesn't matter all that much if a frame made it to the other side of the line, as the person could simply repeat themselves.
TCP on the other hand, is "secure" by default. It performs several handshakes on a regular basis with the endpoint so maintain connectivity and make sure that all packets are received on the other side of the line.
Now, there are a LOT of protocols out there in the Wild Wild West called Internet.
List of TCP and UDP port numbers
As you can see, a lot of protocols support either TCP or UDP. HTTP on it's own is a TCP protocol with port 80 (as you might know). Therefore, HTTPServer is pretty much just an extension of a TCPServer, but with some add-ons such as REST. These add-ons are much welcome as HTTP processing is a pretty common use case. Without HTTPServer, you would need to declare loads of functions on your own.
There are many articles online explaining the difference between HTTP and TCP, so here is: http://www.differencebetween.net/technology/internet/difference-between-tcp-and-http/
Vert.x naturally offers the capacity to do "raw" networking at TCP level or at HTTP-level, the latter offering facilities to deal with the protocol, including decoding TCP packets into HTTTP requests, supporting the creation of HTTP responses, ...
I am having a hard time figuring the motivation behind websockets. From what I've read around the web, regular sockets are still faster and more efficient, so in short, why would I want to use websockets, when and where?
And regarding why to bother with websockets:
It's mainly because browsers only support the Websocket API in their Javascript APIs and do no provide direct TCP socket support.
This was done to prevent (possibly malicious) Javascript apps to create any kind of TCP connections which could provide them confidential information and forward it to the internet.
With Websockets Webapps can only connect to websocket servers. The websocket protocol uses an obfuscation mechanism that prevents that webapps can send any kind of raw TCP data.
Sockets are a lower level. They can work with any type of netwroking. Websockets are at a higher level. They power web servers, and drive web apps. A websocket can be made via plain sockets. It is jsut a lower/higher level thing. Websockets are more of a convience thing. They require you to write less code
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?
To answer your questions.
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.
No. There is no reason.
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
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.
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.
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)
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.