Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Introduction to WebSockets

Wednesday, December 21st, 2016 by Servage

WebSocketsDid you know it is possible to build real-time applications using only PHP? This is possible, thanks to a protocol called WebSocket. WebSockets allow a client and server to remain connected and exchange data in both directions without having to reload a web page. It does not matter whether you are a front-end or back-end developer since WebSockets are available in HTML5, JavaScript, PHP and many more languages and platforms.

How WebSockets work

WebSockets use the TCP protocol on port 80, which is the same port HTTP uses. By definition, WebSocket is a full-duplex protocol, meaning both ends of the connection can talk at the same time without interrupting each other. But how does all of this work?

Because WebSockets utilize TCP, the first step is to create a connection using a TCP handshake. This requires that the port on the receiving end is open. This is why WebSockets use port 80; it is usually open on a web server. When a connection has been established, it remains open on both ends. Because the protocol is full duplex, either computer can start transferring data at any moment. The connection ends when one of the computers disconnects, for example by closing a web page.

Why use WebSockets?

In what scenarios should you use WebSockets instead of a typical website that reloads the page on each request or AJAX? Before we can answer this, we need to find out where the protocol is good at. First of all, WebSockets provide low-latency connectivity because the connection remains open. This can be useful in real-time applications such as online games and chats.

Another advantage is that WebSocket only opens a single connection whereas a typical HTTP connection sends multiple web requests to the same server. Doing everything in a single connection improves performance and reduces bandwidth. If you are building a lightweight application that sends many requests, you should consider implementing it with WebSockets.

With these benefits in mind, you could use WebSockets to create social media or news feeds or even a financial dashboard. Many websites that are built using asynchronous JavaScript could be created with WebSockets too. WebSockets can also be used to communicate between different programming languages. You could for example use WebSockets to create a PHP web control panel that controls servers connected to the panel. The WebSocket connection on the servers can be created with Java, Python or any other language that supports the protocol.

On a side node, WebSocket application are more difficult to create compared to a typical HTTP website. It also takes more time because both ends of the connection must support the communication so it must be implemented twice, and possibly even in two different languages. If you have the time and want to try something new, you should give WebSockets a try and see how well they work for your application.

Introduction to WebSockets, 4.0 out of 5 based on 3 ratings
Categories: Tips & Tricks

Keywords: , , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.