Introduction to Websockets

What is a websocket?

1.1 What type of thing is it?

It is a **connection protocol ** between a server and a client. Another example of a connection protocol is the HTTP protocol.

1.2 What does the protocol look like?

On the server side, one has to register the URL to be used by clients to connect to it. The server can handle events such as “open–which happens when a client connects”, “message–when client message is received”, “close–when the connection is closed”. The server can also send messages to the client.
On the client side, one has to connect to the server by using the URL. The client can handle events such as “open–which happens when the connection is established”, “message–when server message is received”, “close–when the connection is closed”. The client can also send messages to the server.

1.3What are some special properties of this protocol?

Firstly, this protocol is compatible with HTTP. It uses the same port as HTTP (i.e. 80 for unsecure connections and 443 for secure connections). The client can make both normal HTTP and WS requests on the same port to the server! (Do remember that a single client can open many connections with the same server). Each new connection is treated differently based on if it is an HTTP request or a WS request.
Secondly, UNLIKE HTTP, it maintains a full-duplex and persistent connection. HTTP connections are one-way. That means client sends a request and server responds. Half-duplex means communication can only be in one direction at a time. This is very similar to how walkie-talkies function. In contrast, Websockets are full-duplex, which means they allow both sending and receiving to occur at the same time. This is similar to a phone call in which you can talk while the other person is also talking.

2 Why do we want to use websockets?

Reason-1:If you use HTTP, servers have no way to let clients know when some event happens.

For example, maybe some other user just logged in. Maybe someone sent a chat message. Maybe the server is low on resources and is going to be shut down for maintenance. Clients would need to POLL the server to get the latest information. This is not ideal. Using Websockets, the server could just let the client know immediately.

Similarly, when a multi-player game is being implemented, all the player clients would need to be notified of game events quickly.

Reason-2: Most organizations use firewalls but keep allow web connections. Since websockets use web connections (i.e. port 80 etc), applications using websockets would still work.

3 Show me an example of a websocket.

Websocket.org has a simple ECHO server and client implementation. Multiple clients can connect to the websocket server (which is at ws://echo.websocket.org). Note that instead of using http:// we need to use ws:// to connect to use websocket protocol.Here is a short demo of that website. You can do the same.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值