KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ kdb+ and websockets

  • kdb+ and websockets

    Posted by jlucid on February 7, 2023 at 12:00 am

    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

     

     

     

     

    jlucid replied 9 months, 2 weeks ago 2 Members · 3 Replies
  • 3 Replies
  • rocuinneagain

    Member
    February 7, 2023 at 12:00 am

    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

     

  • jlucid

    Member
    February 7, 2023 at 12:00 am

    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

     

  • rocuinneagain

    Member
    February 10, 2023 at 12:00 am

    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)

Log in to reply.