kdb+ and websockets

https://learninghub.kx.com/forums/topic/kdb-and-websockets

The syntax notion for opening a websocket is given as follows

r:(`$“:ws://host:port”)“GET / HTTP/1.1rnHost: host:portrnrn”

but what is the format if I want to connect with a specific URL and pass arguments using ? and &, such as below

localhost:port/ws-replay?exchange=bitmex&from=2019-10-01&to=2019-10-02

how would the top text need to be modified in that case?

I’ve tried adding the additional text to the left and right parts, and both, but is rejecting the connection.

I’ve tested the extended URL with python so I know the arguments are good

 

 

 

 

q connection:

q)wsConn:{x:“/” vs x;h:(`$“:ws://”,x 0) “GET /”,x[1]," HTTP/1.1rnHost: “,x[0],“rnrn”;-1 h 1; h 0} q)wsConn"localhost:8000/ws-replay?exchange=bitmex&from=2019-10-01&to=2019-10-02” HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Date: Tue, 07 Feb 2023 12:12:27 GMT Server: Python/3.8 websockets/10.4 796i

Basic Python websocket server:

import asyncio 
import logging 
import websockets 
logging.basicConfig( format="%(message)s", level=logging.DEBUG, ) 
async def handler(websocket, path): 
    data = await websocket.recv() 
    reply = f"Data recieved as: {data}!" 
    await websocket.send(reply) 
    start_server = websockets.serve(handler, "localhost", 8000) 
    asyncio.get_event_loop().run_until_complete(start_server) 
    asyncio.get_event_loop().run_forever()

Produces logs:

= connection is CONNECTING < GET /ws-replay?exchange=bitmex&from=2019-10-01&to=2019-10-02 HTTP/1.1 < Host: localhost:8000 < Connection: Upgrade < Upgrade: websocket < Sec-WebSocket-Version: 13 < Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== > HTTP/1.1 101 Switching Protocols > Upgrade: websocket > Connection: Upgrade > Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= > Date: Tue, 07 Feb 2023 12:15:48 GMT > Server: Python/3.8 websockets/10.4 connection open = connection is OPEN

 

I found the solution through trial and error, its

:(`$“:ws://localhost:8001”)“GET /ws-replay?exchange=bitmex&from=2019-10-01&to=2019-10-02 HTTP/1.1rnHost: localhost:8001rnrn”

You insert the additional details after the / following the GET command

 

To make reusable as more of a library it’d need a few extensions.

Like if more than one / in the URL.

This would work with 1 or more:

q)wsConn:{i:first where "/"=x;x:(i#x;i _x);(`$":ws://",x 0;"GET ",x[1]," HTTP/1.1rnHost: ",x[0],"rnrn")} 
q)wsConn"localhost:8000/ws-replay/somethingelse?exchange=bitmex&from=2019-10-01&to=2019-10-02" 
`:ws://localhost:8000 
"GET /ws-replay/somethingelse?exchange=bitmex&from=2019-10-01&to=2019-10-02 HTTP/1.1rnHost: localhost:8000rnrn"

Would also need to account for

a) No / in the URL

b) Embedded / in the options (if allowed)