Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Websockets

...

Websockets are connections between a client and a webserver. The client can create a regular request to a webserver (an HTTP GET) and request to upgrade the connection to a bidirectional TCP connection. That connection will then remain open for as long as you want.

...

NetTalk webserver can be extended to support websockets. In this article I'll show you exactly how that's done. 

Amsterdam Taxi service

I'll start with a demo to illustrate some of the possibilities of websockets. Imagine that you're running a taxi service in the center of Amsterdam. All taxis are equipped with a GPS enabled smart phone. Using websockets, those devices can send their location every second without overloading the webserver because the websocket protocol hardly has any overhead.

...

The fluency of this interface can only be achieved by websockets. It goes without saying that when taxis move around through the city, having the positions update as often as possible is a must have. 

So, how does it work? 

The taxi app is a regular NetTalk webserver application, extended with websocket capabilities. Below I'll explain extactly how the taxi application uses those websockets.

IndexPage

The indexPage contains code to allow a connection to be upgraded to a websocket. To initiate a websocket, a browser will send a normal GET request with some extra HTTP headers. If those headers are present, the IndexPage will upgrade the connection with the webserver to a websocket by calling the Add() method of the global WebSocketServerClass.

...

This class only handles the message events. event.data contains the actual message sent by the server. In this particular application, all messages are in JSON format which is why the message is parsed to a JSON structure. The message doesn't have to be in JSON format though, you can just as easily send semi-colon separated strings or whatever else suits you best.  

WebSocketsHandler

The WebSocketsHandler procedure is the websocket equivalent of NetTalk's Webhandler. It will be called for every websocket request and can run simultaneously on multiple threads. 

...

All these methods are threadsafe 

TaxiMapClass

The TaxiMapClass is a global class that wraps two queues and contains the code to send messages to all taxis. One queue contains all taxis, the other queue contains the rides. 

...

Your NetTalk webserver is now websocket aware. It doesn't do anything useful though. It just accepts websocket requests and will receive all messages that the attached clients send. 

A basic implementation of websockets

Just embed code into the WebsocketRequestHandler procedure to start leveraging this awesome functionality. Let's do something pretty basic: broadcast all messages to all clients: 

...

As soon as you open this url in a second browser, this browser window will be immediately notified that a new client has connected. All messages that you send from either window, will appear in both windows. And there you have it, a very simple websocket application!

Want to see more?

This taxi demo and a working version of the demo above are available for download: http://www.indirection.nl/downloads/websockets.zipThere are several demo applications available several other demo applications demonstrating some of the use cases for websockets (such as a concurrency checker with an online chatting option, the taxi demo and a shoppingcart shopping cart with online help. The demos can be downloaded from http://www.indirection.nl/downloads/) are available for download:

Download websockets.zip

I would also like to encourage you to view ClarionLive session #291 that discusses all the demos. 

Please note that the download does not contain the websocket classes nor the websocket templates and that you will therefore not be able to compile the demos. The compiled versions of the demos are included, so you will be able to run the demos on your machine. 

How to obtain the websockets classes

The websocket classes require NetTalk 8, StringTheory and Cryptonite.

...