cancel
Showing results for 
Search instead for 
Did you mean: 

kdb+ and websockets

jlucid
New Contributor III

The syntax notion for opening a websocket is given as follows

r:(`$":ws://host:port")"GET / HTTP/1.1\r\nHost: host:port\r\n\r\n"

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

 

 

 

 

1 ACCEPTED SOLUTION

q connection:

q)wsConn:{x:"/" vs x;h:(`$":ws://",x 0) "GET /",x[1]," HTTP/1.1\r\nHost: ",x[0],"\r\n\r\n";-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

 

View solution in original post

4 REPLIES 4

jlucid
New Contributor III

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.1\r\nHost: localhost:8001\r\n\r\n"

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

 

q connection:

q)wsConn:{x:"/" vs x;h:(`$":ws://",x 0) "GET /",x[1]," HTTP/1.1\r\nHost: ",x[0],"\r\n\r\n";-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

 

jlucid
New Contributor III

Thanks Rian, I'll use wsConn

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.1\r\nHost: ",x[0],"\r\n\r\n")}
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.1\r\nHost: localhost:8000\r\n\r\n"

 Would also need to account for a) No / in the URL b)  Embedded / in the options (if allowed)