#Websocket client with headers

36 messages · Page 1 of 1 (latest)

uneven drum
#

I'm trying to create a websocket client that has an authorization header. It seems that a lot of the functionality of websockets has been moved out of the standard library, and I don't see much in the core. Any advice on how to proceed?

For reference, I'm trying to do the following in Deno, rather than by using websocat:
https://trading-api.readme.io/reference/introduction#connecting

nova geyser
#

they are using this header in unintended way 😛

#

I believe it would look like this xD

#
new WebSocket("", ["Authorization: Bearer safoinasfoiasfoih"])
#

because this header is used to specify "inner?" protocol

uneven drum
#

Interesting, when I try that I get an "Invalid protocol value" runtime error

nova geyser
#

as I've said - they made it working like this server-side

uneven drum
#

I see

nova geyser
#

they read and parse that "as protocol"... tbh it is genious if it is not somewhat insecure xD

uneven drum
#

haha

#

Tools like websocat an uwsc have a headers flag, are they passing it as a protocol?

nova geyser
#

no, w8, looks like I am uneducated, take a look here:

#

they talk about authing your ws connection here

#

hope it helps ^^

uneven drum
#

I'll take a look - thanks for the help!

nova geyser
#

^^

uneven drum
#

From the article:

If the client doesn’t support sending authorization headers, the secure token can be sent in URL:

> const ws=new WebSocket('ws://localhost:5000?accessToken=TOKEN-1');
> ```

How do you think you would send `Authorization: Bearer ${token}` in the URL?
#

I think this is probably done on the server, too. So I don't think this is a general work around it, but rather a way around it if you control the client and server

#

I'm thinking that it's probably that I need to make an HTTP request to the server and then upgrade the connection. I'm not exactly sure how to do that yet...

nova geyser
#

TOKEN-1 is your token

uneven drum
#

I think I tried that, but let me make sure

#

Yeah, it doesn't seem to work. I'm getting an authorization error trying to open the websocket (I'm able to connect with websocat with the same token)

nova geyser
#

If you can open your source or invite me - I would be interested in what are you making 😄

#

I mean

#

at this point most likely you know abot this topic more than me

#

when I had to do this stuff I had some flow like this...
I had "please give me session link" rest route, it gave you presigned url for websocket

#

and I was assuming that the client that knows this random route is the same who asked on the rest route

uneven drum
#

I'm making a bot to run on Kalshi. At this point, there's not much to show. I have something that uses requests and makes trades, but they just recently introduced their websocket, and having an event-driven bot seems much better than a polling one.

At the moment, I'm just trying to get the websocket to work on its own.

Here's the code I'm working from

import "https://deno.land/x/dotenv@v3.2.0/load.ts";
import * as z from "https://deno.land/x/zod@v3.16.1/mod.ts";

const kalshiToken = z.string().parse(Deno.env.get('KALSHI_API_TOKEN'))
const kalshiUrl = 'wss://demo-api.kalshi.co/trade-api/ws/v2'

const url = `${kalshiUrl}?accessToken=${kalshiToken}`
const ws = new WebSocket(url)

ws.onopen = (event) => {
  console.log('Connected to Kalshi', event)
}

ws.onclose = (event) => {
  console.log('Disconnected from Kalshi', event)
}

ws.onmessage = (event) => {
  console.log('Message from Kalshi', event)
}

ws.onerror = (event) => {
  console.log('Error from Kalshi', event)
}
uneven drum
nova geyser
uneven drum
#

Haha, thanks for your help so far!

nova geyser
#

@uneven drum ^

#

(Haven't tried it ever myself Kappa)

uneven drum
#

Ah, maybe that's what I'm looking for - thanks for finding it!