For those of you interested in websockets, kdb+ 3.2 can now act as a client, in addition to as a server. Documented here
http://code.kx.com/wiki/Cookbook/Websocket
This is an interesting way to connect to websocket services, e.g. pusher.com or bitcoin exchanges etc.
Added websocket client functionality. .z.ws must be defined before opening a websocket, e.g.
q).z.ws:{0N!x;} / print incoming msgs to console, no echo.
Then open a websocket, e.g.
q)r:(`quot;:ws://host:port")"“GET / HTTP/1.1\r\nHost: host:port\r\n\r\n”
If successful it will return a 2 element list of (handle;http response), e.g.
(3i;“HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Extensions: permessage-deflate\r\n\r\n”)
and from that point on will callback via .z.ws when msgs arrive. To send msgs, use neg[handle]“text” or neg[handle]byteVector.
If the protocol upgrade from http to websocket failed, it returns the 2 element list, with the handle as 0Ni, e.g.
(0Ni;“HTTP/1.1 400 Bad Request\r\nContent-Type: text/html; charset=UTF-8…”)
The response text is returned for debug purposes only; ideally, you need only be concerned whether the handle is valid.
Any other error is thrown as usual, e.g.
'www.nonexist.badcom: No route to host
Should you need to use websockets over ssl, e.g. wss://host:port, consider stunnel, and open from kdb+ to that stunnel with ws://.
Basic Authentication can be passed in the char vector on opening, along with any other necessary fields such as cookies etc.
An example session -
q).z.ws:{0N!x;}
q)`:ws://ws.blockchain.info:80 “GET /inv HTTP/1.1\r\nHost: ws.blockchain.info\r\n\r\n”
3i
“HTTP/1.1 101 Switching Protocols\r\nServer: nginx\r\nDate: Sat, 26 Jul 2014 09:53:02 GMT\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\n\r\n”
q)neg[3]“{"op":"unconfirmed_sub"}”
then, after a few secs, callback prints
"{"op":"utx","x":{"hash":"0b3dc1108477539c3b635b30e4e67630b7e038c65a013efb62653276216b8267","vin_sz":1,"vout_sz":2,"lock_time":"Unavailable","size":226,"relayed_by":"188.226.161.239","tx_index":61038847,"time":1406368531,"inputs":[{"prev_out":{"value":5192700,"addr":"166wZYCWrBwj1eppfbV8K4aJV2pRtsaY4d","type":0}}],"out":[{"value":5082700,"addr":"1PXZ8fpGKnWH9tUucbXGciiJ7kMW4yiACq","type":0},{"value":100000,"addr":"1LuckyR1fFHEsXYyx5QK4UFzv3PEAepPMK","addr_tag":"LuckyBit red","addr_tag_link":"[http://luckyb.it/\](“http://luckyb.it/\“)”,\“type\”:0}]}}”