-
-
Save suenot/e28522f55c89c1e9b0052b384ea6a6ac to your computer and use it in GitHub Desktop.
Tarantool + lua-http websockets nonblocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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