Audrow
Audrowβ€’2y ago

Websocket client with headers

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
Kalshi Developer Suite
Market Data Feed
Protocol overview Our websockets api is only served over an encrypted connection. There is a single endpoint to establish WS connections: wss://trading-api.kalshi.com/trade-api/ws/v2. We provide multiple channels of information over a single connection. So after establishing a WS connection, the cli...
21 Replies
Danielduel
Danielduelβ€’2y ago
they are using this header in unintended way πŸ˜› I believe it would look like this xD
new WebSocket("", ["Authorization: Bearer safoinasfoiasfoih"])
new WebSocket("", ["Authorization: Bearer safoinasfoiasfoih"])
because this header is used to specify "inner?" protocol
Audrow
AudrowOPβ€’2y ago
Interesting, when I try that I get an "Invalid protocol value" runtime error
Danielduel
Danielduelβ€’2y ago
as I've said - they made it working like this server-side
Audrow
AudrowOPβ€’2y ago
I see
Danielduel
Danielduelβ€’2y ago
they read and parse that "as protocol"... tbh it is genious if it is not somewhat insecure xD
Audrow
AudrowOPβ€’2y ago
haha Tools like websocat an uwsc have a headers flag, are they passing it as a protocol?
Danielduel
Danielduelβ€’2y ago
no, w8, looks like I am uneducated, take a look here:
Danielduel
Danielduelβ€’2y ago
they talk about authing your ws connection here hope it helps ^^
Audrow
AudrowOPβ€’2y ago
I'll take a look - thanks for the help!
Danielduel
Danielduelβ€’2y ago
^^
Audrow
AudrowOPβ€’2y ago
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');
>
> 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...
Danielduel
Danielduelβ€’2y ago
TOKEN-1 is your token
Audrow
AudrowOPβ€’2y ago
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)
Danielduel
Danielduelβ€’2y ago
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
Audrow
AudrowOPβ€’2y ago
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)
}
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)
}
That might be the way to go - I'm new to using websockets so I'm very much trying to figure out how to use them at all
Danielduel
Danielduelβ€’2y ago
I wish you only profits then πŸ˜„
Audrow
AudrowOPβ€’2y ago
Haha, thanks for your help so far!
Danielduel
Danielduelβ€’2y ago
There is something like this https://deno.land/api@v1.29.1?unstable&s=WebSocketStream but. You have to enable unstable features, options here take headers
Deno
WebSocketStream | Runtime APIs | Deno
UNSTABLE: New API, yet to be vetted.
Danielduel
Danielduelβ€’2y ago
@audrow ^ (Haven't tried it ever myself Kappa)
Audrow
AudrowOPβ€’2y ago
Ah, maybe that's what I'm looking for - thanks for finding it! I got it working - thanks for the help @danielduel!