libwebsockets: Simple WebSocket server

if you’re trying to use libwebsockets you probably want to build your own awesome WebSocket server.
I’ve already made a tutorial on how to make a web server using libwebsocket and finally now I’m going to make a very simple WebSocket server. At first, I wanted to make just a “port” of my previous tutorial on Node.js but this would be I think much more complicated and I wanted to keep this as simple as possible.

But still I recommend you to read the first tutorial about building a web server because there are some insight of how does libwebsocket work and it’ll be easier to understand what’s going on here.

BTW, if you’re looking for some more in-depth information on how WebSockets work I recommend this article Websockets 101.

So, to keep it very simple our WebSocket server will just respond to every request you send it reverse order. For example if we send “Hello, world!” it will respond “!dlrow ,olleH”.

WebSocket server

The first part is very similar to the web server, but since we don’t want to handle any HTTP requests the callback_http may be stay empty.

The interesting part comes now. We need to handle each request send a response.

I tried to comment the source code where I think it’s appropriate. The most important method is libwebsocket_write and the way it send response back to the client.

Also void *in and size_t len which holds the request and its length respectively are worth mentioning.

In the next part we define all protocols we’re using. The first protocol has to be HTTP, then we can put whatever we want. Pay attention to the WebSocket protocol name.

The W3C official WebSocket definition says that WebSocket constructor takes two arguments. WebSocket server URL and some optional protocol name (or subprotocol if you want). I didn't know what is the second argument for until I started using libwebsockets. For us the protocol name is dumb-increment-protocol and we'll use it later in the JavaScript part.

The rest is the same like in the web server tutorial.

At this moment you can compile and run the server (see compiling tutorial). You should see something like this:

Frontend

Now it’s finally time to write some JavaScript. In order to keep it very simple I’m putting everything in one HTML file:

I think it’s obvious what it does and why. The most surprising part is probably:

where dumb-increment-protocol is the subprotocol name that we specified in static struct libwebsocket_protocols protocols[] = { … }.

If you think about it, it means that one WebSocket server can run infinite number of different WebSocket protocols absolutely independent on each other on the same port. Well, that’s interesting to me.

Now open the index.html and if connection to our WebSocket server was successful you should see green heading "WebSocket test" like in the image bellow. Write some clever message in the input field and hit Enter. Our WebSocket server takes your message and sends it back. Also you should see log messages in the server's console too.

Complete source codes are as usually on gist.github.com, feel free to send pull requests if you find a bug or if you have an idea how to make it even better!

Conclusion

I believe it wasn’t that horrible.

By the way, if you use libwebsockets for some real application say thank you to http://warmcat.com who created it.

One more thing, I discovered that there’s a forked version of libwebsockets with some additional features. I haven’t tested it yet, but I will as soon as I can and I’ll update this article with more info.