| 1 | -- Change these: |
| 2 | local jid, password = "user@example.com", "secret"; |
| 3 | |
| 4 | -- This line squishes verse each time you run, |
| 5 | -- handy if you're hacking on Verse itself |
| 6 | --os.execute("squish --minify-level=none"); |
| 7 | |
| 8 | require "verse".init("client"); |
| 9 | |
| 10 | c = verse.new(); |
| 11 | c:add_plugin("pubsub"); |
| 12 | |
| 13 | -- Add some hooks for debugging |
| 14 | c:hook("opened", function () print("Stream opened!") end); |
| 15 | c:hook("closed", function () print("Stream closed!") end); |
| 16 | c:hook("stanza", function (stanza) print("Stanza:", stanza) end); |
| 17 | |
| 18 | -- This one prints all received data |
| 19 | c:hook("incoming-raw", print, 1000); |
| 20 | |
| 21 | -- Print a message after authentication |
| 22 | c:hook("authentication-success", function () print("Logged in!"); end); |
| 23 | c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end); |
| 24 | |
| 25 | -- Print a message and exit when disconnected |
| 26 | c:hook("disconnected", function () print("Disconnected!"); os.exit(); end); |
| 27 | |
| 28 | -- Now, actually start the connection: |
| 29 | c:connect_client(jid, password); |
| 30 | |
| 31 | -- Catch the "ready" event to know when the stream is ready to use |
| 32 | c:hook("ready", function () |
| 33 | print("Stream ready!"); |
| 34 | |
| 35 | -- Create a reference to a node |
| 36 | local node = c.pubsub("pubsub.shakespeare.lit", "princely_musings"); |
| 37 | |
| 38 | -- Callback for when something is published to the node |
| 39 | node:hook(function(event) |
| 40 | print(event.item) |
| 41 | end); |
| 42 | node:subscribe() -- so we actually get the notifications that above callback would get |
| 43 | |
| 44 | node:publish( |
| 45 | nil, -- no id, so the service should give us one |
| 46 | nil, -- no options (not supported at the time of this writing) |
| 47 | verse.stanza("something", { xmlns = "http://example.com/pubsub-thingy" }) -- the actual payload, would turn up in event.item above |
| 48 | :tag("foobar"), |
| 49 | function(success) -- callback |
| 50 | print("publish", success and "success" or "failure") |
| 51 | end) |
| 52 | end); |
| 53 | |
| 54 | print("Starting loop...") |
| 55 | verse.loop() |