After facing problem with the historical API from a week now.
Today I started facing problem with the websocket where I am receiving very few ticks.
After facing issue for the whole day on my application, I test it on the postman, and I didn’t get even a single tick until the market was closed at 3:30pm I got some data.
Please check the attached image from the postman websocket connection where I am connected to websocket and have send the request for bank nifty index at full mode.
Message I sent after successful connection:
{
“guid”: “13syxu852ztodyqncwt0”,
“method”: “sub”,
“data”: {
“mode”: “full”,
“instrumentKeys”: [“NSE_INDEX|Nifty Bank”]
}
}
@Ketan@Ushnota Please fix this asap I am not able to run my strategies because of this.
Hi @Kautilya, After the successful connection, the subscription data must be sent in binary format. Please refer to the sample WebSocket implementation in python here for guidance.
@Anand_Sajankar As you can see from the image, I didn’t received any kind of data no binary data or in any other format.
I have been using the websockets in my application, and I know the data is send in binary format, but today there has been no data after subscription.
Kindly check why did this happen.
@Anand_Sajankar facing the same issue today also, No getting any data from websocket.
I have susbcribed with the nifty 50 and bank nifty data, but not receiving any data at all.
Please fix this asap, I am not able to run my strategies.
@Anand_Sajankar
I am sending the data in binary only on postman but still no ticks are coming from the websocket.
I am connected successfully with the websocket and even after sending the message in binary format I am not getting any response back from the websocket.
Here, I am also facing the issue related to web socket. I am successfully connecting with web socket and also receiving market feed as well but not getting further feeds like live feed. Can you please suggest me proper solution for it.
Here is the my code snipt which I have implemented for web socket.
def run(cron_job = true, run_by = "scheduler")
cron_data = CronData.create(start_at: Time.now, cron_title: "Sockets::UpstoxClient.start_socket_connection", run_by: run_by)
begin
EM.run {
if EM.connection_count < 1
WebsocketActivity.where(status: "open").update_all(stopped_at: DateTime.now, status: "stopped")
web_socket_activity = WebsocketActivity.create!(title: "Sockets::UpstoxClient.run", status: "open", start_at: DateTime.now)
token = "Bearer #{ENV['UPSTOX_ACCESS_TOKEN']}"
redirect_url = get_redirect_url(token)
ws = Faye::WebSocket::Client.new(redirect_url,
['json'],
headers: {
'Authorization' => token,
'Accept' => '*/*'
}
)
symbol_keys = ['NSE_INDEX|Nifty 50'] #CacheModule.get_all_upstox_instrument_keys
puts "symbol_keys: #{symbol_keys}"
ws.on :open do |event|
puts "✅ Upstox WebSocket Connected"
subscription_payload = {
guid: SecureRandom.uuid,
method: 'sub',
data: {
mode: 'ltpc',
instrumentKeys: symbol_keys #['MCX_FO|220230']
}
}
ws.send(subscription_payload.to_json)
# EM.add_periodic_timer(30) {
# puts "🫀 Heartbeat at #{Time.now}"
# # Upstox doesn’t require ping unless explicitly stated
# }
puts "📩 Sent subscription"
end
ws.on :message do |event|
begin
puts "📡 Received binary data"
# Decode using protobuf here
raw = event.data
binary_data = raw.is_a?(Array) ? raw.pack('C*') : raw
decoded = Com::Upstox::Marketdatafeederv3udapi::Rpc::Proto::FeedResponse.decode(binary_data)
puts "Decoded data #{decoded}"
puts "📥 Raw binary size: #{binary_data.bytesize} bytes"
puts "📦 Decoded type: #{decoded.type}"
puts "📊 Instruments in feed: #{decoded.feeds.keys}"
rescue JSON::ParserError => e
puts "JSON Parse Error: #{e.message}"
ActionCable.server.broadcast "messages", { error: "Error parsing message" }
end
end
ws.on :close do |event|
web_socket_activity.update!(status: "closed", stopped_at: DateTime.now)
puts "❌ Upstox WebSocket Closed (#{event})"
ws = nil
EM.stop
StockCompanies::UpstoxWebsocketClientStartWorker.perform_at(30.seconds.from_now)
end
ws.on :error do |e|
puts "❌ WebSocket Error: #{e.message}"
end
end
}
cron_data.update(status: "success", end_at: Time.now)
rescue => e
cron_data.update(status: "error", error: e.message, end_at: Time.now)
puts "⚠️ WebSocket Exception: #{e.message}"
end
end
private
def get_redirect_url(token)
url = URI("https://api.upstox.com/v3/feed/market-data-feed/authorize")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = token
request["Accept"] = "application/json"
response = https.request(request)
JSON.parse(response.read_body)["data"]["authorized_redirect_uri"]
end