Software / code / verse
Comparison
doc/example_pubsub.lua @ 230:44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 06 Nov 2011 21:09:07 +0100 |
| child | 260:7f6df45a3d1f |
comparison
equal
deleted
inserted
replaced
| 229:279c0e89c3b3 | 230:44a6da432e7e |
|---|---|
| 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" -- Verse main library | |
| 9 require "verse.client" -- XMPP client library | |
| 10 | |
| 11 c = verse.new(); | |
| 12 c:add_plugin("pubsub"); | |
| 13 | |
| 14 -- Add some hooks for debugging | |
| 15 c:hook("opened", function () print("Stream opened!") end); | |
| 16 c:hook("closed", function () print("Stream closed!") end); | |
| 17 c:hook("stanza", function (stanza) print("Stanza:", stanza) end); | |
| 18 | |
| 19 -- This one prints all received data | |
| 20 c:hook("incoming-raw", print, 1000); | |
| 21 | |
| 22 -- Print a message after authentication | |
| 23 c:hook("authentication-success", function () print("Logged in!"); end); | |
| 24 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end); | |
| 25 | |
| 26 -- Print a message and exit when disconnected | |
| 27 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end); | |
| 28 | |
| 29 -- Now, actually start the connection: | |
| 30 c:connect_client(jid, password); | |
| 31 | |
| 32 -- Catch the "ready" event to know when the stream is ready to use | |
| 33 c:hook("ready", function () | |
| 34 print("Stream ready!"); | |
| 35 | |
| 36 -- Create a reference to a node | |
| 37 local node = c:pubsub("pubsub.shakespeare.lit", "princely_musings"); | |
| 38 | |
| 39 -- Callback for when something is published to the node | |
| 40 node:hook(function(event) | |
| 41 print(event.item) | |
| 42 end); | |
| 43 node:subscribe() -- so we actually get the notifications that above callback would get | |
| 44 | |
| 45 node:publish( | |
| 46 nil, -- no id, so the service should give us one | |
| 47 nil, -- no options (not supported at the time of this writing) | |
| 48 verse.stanza("something", { xmlns = "http://example.com/pubsub-thingy" }) -- the actual payload, would turn up in event.item above | |
| 49 :tag("foobar"), | |
| 50 function(success) -- callback | |
| 51 print("publish", success and "success" or "failure") | |
| 52 end) | |
| 53 end); | |
| 54 | |
| 55 print("Starting loop...") | |
| 56 verse.loop() | |
| 57 |