Erlang Cowboy WebSocket Server
The original text is:
Http://marcelog.github.io/articles/erlang_websocket_server_cowboy_tutorial.html
This article is not a simple translation of the original text, is a reference to the original, according to my understanding and practice written. The source code of this article is:
Https://github.com/marcelog/erws
1 Introduction
Erlang can be used to implement a WebSocket server. Cowboy such a framework can accomplish this task, is that we do not have to focus on the details of the WebSocket protocol. Before we have a meal, we'll start with a few discs:
WebSocket is a network technology that HTML5 started to provide full duplex communication between the browser and the server. The WebSocket communication protocol was established as the standard RFC 6455,websocketapi by the IETF in 2011. In the WebSocket API, browsers and servers only need to act as a handshake, and then a fast channel is formed between the browser and the server. The data can be transmitted to each other directly between the two. Now, many Web sites are polled (polling) in order to achieve instant messaging (real-time). Polling is at a specific interval (time interval) (for example, every 1 seconds), the browser sends an HTTP request to the server, and the server returns the latest data to the client's browser. This traditional HTTP request D pattern has obvious drawbacks – the browser needs to make a request to the server constantly, but the header of the HTTP request is very long, and the data contained in it may be a small value, which consumes a lot of bandwidth. And the most new technology to do polling is the long poll (long polling), which is very old technology. The advantage of long polling is that it completely overturns the typical client-sent request approach. The core of long polling is the interaction between the client and the server, breaking the traditional convention that clients send requests as quickly as possible. The client expects the server to retain the request for a long time, maintaining the openness of the potential TCP connection. As long as the server has the information it wants to share with the client, it transmits the data to the client and terminates the connection. This approach is relatively efficient and eliminates the hassle of all HTTP requests. After the client receives the update data, it sends the next request to the server and expects the server to retain the request until it has the data to be returned. Long-time connection based on HTTP is a technology of "server push" through long polling, which makes up the insufficiency of HTTP simple request answering mode, and greatly enhances the real and interactive of the program. Long connections, long polling general applications with Webim, chatroom and some Web applications that need to interact in a timely manner. Its real cases are: WEBQQ, HI Web version, Facebook im and so on. And in the WebSocket API, the browser and the server only need to do a handshake, and then, the browser and the server formed a fast channel. The data can be transmitted to each other directly between the two. In this WebSocket agreement, we realize that even the service brings two great benefits: header, communication header is very small-about 2 bytesserver Push, the server can actively transmit data to the client. So websOcket is a newer and better technology.
2 Why Use Cowboy
Cowboy is the ultimate Titans. So far (2012-05), the Erlang tools that can be used to write WebSocket services are:
Socket.io-erlang
This is the most attractive method because it can be used on the client. Socket.io is very popular, especially in Nodejs, because one of the authors (Frederic Trottier-hebert) is also the author of Learnyousomeerlang. But since Socket.io 0.6.x, the author no longer adds new features. So it's very bad for this product.
MochiWeb
The HTTP framework is very popular, but it does not natively support websocket.
Yaws
This is a full-featured HTTP server that is robust enough and extensible. Applies to both static and dynamic pages, and also supports WebSockets. But it's a little overkill for my needs. Of course, if no other solution works, I will definitely choose this. (Translator Note: Yaws is also my next to learn to use the product)
Misultin
The original author says no more updates, and has turned to mochiweb and cowboy.
Cowboy
Very interesting stuff. HTTP is part of this product framework. This thing is more like a connection pool, we can set up to handle TCP or SSL connections. The biggest drawback is the lack of documentation, and sometimes the need to look at the source code.
3 Start programming
The original text uses rebar to build the project. Rebar is a built-in Erlang tool that makes it easy to compile, test Erlang programs, inline drivers, and package Erlang releases.
"Translator: I continue to use the Make tool for the previous article (Erlang Cowboy Primer Reference), so this article is slightly out of the original"
This time, we create a ERWS (Erlang WebSocket), the main module is Erws_handler.erl, all the necessary files will be shown below.
When the program runs, Cowboy listens on port 10100, distributing HTTP requests to Erws_handler.
The Erws_handler implements 2 processors: Cowboy_http_handler and Cowboy_websocket_handler. The first processor accepts a normal HTTP GET request and starts websocket to complete the handshake by sending the necessary header information to make the HTTP connection upgrade (upgrade), as described in the protocol. For us, the process is very simple and we only need to return a tuple to the ERWS_HANDLER:INIT/1 (reference code). All dirty work is done by the Cowboy module Cowboy_http_websocket.
Once this process is completed, by invoking the API exposed by Cowboy_http_websocket_handler, we can treat the received request as message processing, very similar to the normal gen_server.
Erlang Cowboy WebSocket Server