Skip to content

Instantly share code, notes, and snippets.

@kn666
Forked from daurnimator/tarantool-websockets.lua
Created April 15, 2018 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn666/a0ff23783ea7018c6f17243ac51ed64e to your computer and use it in GitHub Desktop.
Save kn666/a0ff23783ea7018c6f17243ac51ed64e to your computer and use it in GitHub Desktop.
Tarantool + lua-http websockets nonblocking
#!/usr/bin/env tarantool
local cqueues = require "cqueues"
local fiber = require "fiber"
local socket = require "socket"
package.loaded["http.client"] = nil -- tarantool has a namespace clash
local websocket = require "http.websocket"
local cq = cqueues.new()
-- Hook up cqueues loop inside tarantool fiber
fiber.create(function()
while true do
local ok, err = cq:step(0)
if not ok then
-- Error running cqueues thead. print error and continue
print(err)
end
socket.iowait(cq:pollfd(), cq:events(), cq:timeout())
end
end)
cq:wrap(function()
local ws = websocket.new_from_uri("wss://ws-feed.gdax.com")
assert(ws:connect())
assert(ws:send([[{"type": "subscribe", "product_id": "BTC-USD"}]]))
for _=1, 5 do
local data = assert(ws:receive())
print(data)
end
assert(ws:close())
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment