Audrow
Audrow
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
I got it working - thanks for the help @danielduel!
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
Ah, maybe that's what I'm looking for - thanks for finding it!
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
Haha, thanks for your help so far!
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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)
}
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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)
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
I think I tried that, but let me make sure
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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...
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
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?
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
I'll take a look - thanks for the help!
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
Tools like websocat an uwsc have a headers flag, are they passing it as a protocol?
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
haha
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
I see
37 replies
DDeno
Created by Audrow on 12/17/2022 in #help
Websocket client with headers
Interesting, when I try that I get an "Invalid protocol value" runtime error
37 replies