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...
D
Danielduel492d 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
A
Audrow492d ago
Interesting, when I try that I get an "Invalid protocol value" runtime error
D
Danielduel492d ago
as I've said - they made it working like this server-side
A
Audrow492d ago
I see
D
Danielduel492d ago
they read and parse that "as protocol"... tbh it is genious if it is not somewhat insecure xD
A
Audrow492d ago
haha Tools like websocat an uwsc have a headers flag, are they passing it as a protocol?
D
Danielduel492d ago
no, w8, looks like I am uneducated, take a look here:
D
Danielduel492d ago
D
Danielduel492d ago
they talk about authing your ws connection here hope it helps ^^
A
Audrow492d ago
I'll take a look - thanks for the help!
D
Danielduel492d ago
^^
A
Audrow492d 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...
D
Danielduel492d ago
TOKEN-1 is your token
A
Audrow491d 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)
D
Danielduel491d 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
A
Audrow491d 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
D
Danielduel491d ago
I wish you only profits then 😄
A
Audrow491d ago
Haha, thanks for your help so far!
D
Danielduel491d 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.
D
Danielduel491d ago
@audrow ^ (Haven't tried it ever myself Kappa)
A
Audrow491d ago
Ah, maybe that's what I'm looking for - thanks for finding it! I got it working - thanks for the help @danielduel!
More Posts
Can I programatically trigger a SIGINT event?I would like to trigger a SIGINT event from a file, is there a way to do this? For example in Node, i think the language server is brokenwas working perfectly fine last night, and im not sure how to fixHow to forward a reference to a componentI created this sample to exemplify what I don't understand. Using the Fresh framework, I created the[Fixed] Package cannot read a file from itself when loaded from urlI have a package A, which has to read an html file located at the package's root, such as the followhow can I enable Deno vscode all inlay hints at once?There seem to be too many config options that I have to set to `true`. Is there a way to just enable--max-stack-sizeI can set the maximum call stack size on deno?Deno Deploy --cert flagI am using deno fresh and supabase postgres. When in local environment I use --cert flag to set ssl What is the best database service to plug into your application hosted on Deno Deploy?Deno Deploy is hosted on Google Cloud, but I was wondering: is a database service from another compaHow can I create connections pool for postgres with denodb?How can I create connections pool for postgres with denodb?Fresh and MDXWhat is most proper way of using mdx with freshfresh kanalına mesaj gönderTTY vs console.log vs stdin and setRaw for dummies?Hi all! Would someone be able to clear some things out with regards to deno inputs and outputs: - wIs there an event that triggers when an Oak Deploy instance is closing?I cannot find anything in the Deploy docs, specifically. But I see that the Oak `app` runs a callbac