Simple Corona SDK example - cannot subscribe via pubnub console

I'm trying to run Helloworld's default pubnub example using Corona. The corona sdk program works fine in the simulator, but I can't see the output in the pubnub console for some reason. Here is the source for the pubnub Corona SDK.

require "pubnub"

multiplayer = pubnub.new({
    publish_key   = "my_publish_key",
    subscribe_key = "my_subscribe_key",
    secret_key    = nil,
    ssl           = nil,
    origin        = "pubsub.pubnub.com"
})

multiplayer:subscribe({
    channel  = "hello-world-corona",
    callback = function(message)
      print(message.msgtext)
    end,
    errorback = function()
      print("Oh no!!! Dropped 3G Conection!")
    end
})

function send_a_message(text)
  multiplayer:publish({
     channel = "hello-world-corona",
     message = { msgtext = text }
   })
end

function send_hello_world()
  send_a_message("Hello World!!!")
end

timer.performWithDelay( 500, send_hello_world, 10 )
send_hello_world()

      

I am using pubnub providing subscription and publishing keys

To generate the API key, I used these libraries from pubnub: https://github.com/pubnub/lua

The problem is that when I build an Android APK and run it, I cannot see messages in the pubsub console at http://www.pubsub.com/console . I am using the specified subscriptions and posting the keys, but I can't see anything. I was getting runtime error for internet permission but then changed build.settings settings to the following and no longer got runtime error:

build.settings:

   settings =
{ 
android =
   {
    permissions =
        {
            { name = ".permission.C2D_MESSAGE", protectionLevel = "signature" },
        },
        usesPermissions =
        {
           -- Required by the MapView to fetch its contents from the Google Maps 
           --servers.
            "android.permission.INTERNET",
            "android.permission.GET_ACCOUNTS",
            "android.permission.RECEIVE_BOOT_COMPLETED",
            "com.google.android.c2dm.permission.RECEIVE",
            ".permission.C2D_MESSAGE",


          -- Optional permission used to display current location via the GPS.
            "android.permission.ACCESS_FINE_LOCATION",

          -- Optional permission used to display current location via WiFi or 
          -- cellular   
          -- service.
          "android.permission.ACCESS_COARSE_LOCATION",
      },
      usesFeatures =
      {
          -- If you set permissions "ACCESS_FINE_LOCATION" and 
          --"ACCESS_COARSE_LOCATION" above, then you may want to set up 
          --your app to not require location services as follows.
          -- Otherwise, devices that do not have location sevices (such as a GPS) will 
          -- be unable
          -- to purchase this app in the app store.
            { name = "android.hardware.location", required = false },
            { name = "android.hardware.location.gps", required = false },
            { name = "android.hardware.location.network", required = false },
        },
   }, 
}

      

+3


source to share


2 answers


Please add success and error callbacks, and let us know in your question what you get, and whether from its success or from a callback.

Examples of successful and error callback implementations can be found https://github.com/pubnub/lua/blob/master/corona/examples/example-publish/main.lua#L29



Geremy

0


source


PubNub Corona SDK Debugging Publishing Events

Be sure to register a callback error

to detect issues with publish-related events. Below is an example of a fully functional call . pubnub.publish({...})

-- 
-- CALL PUBLISH FUNCTION
--
function publish( channel, text )
    pubnub_obj:publish({
        channel  = channel,
        message  = text,
        callback = textout,
        error    = textout
    })
end

      

Full Lua Publishing Demo:



Use this to test your lua corona application to post messages successfully. You will see text output for debugging your code.

--
-- PubNub : Publish Example
--

require "pubnub"
require "PubnubUtil"

textout = PubnubUtil.textout

--
-- INITIALIZE PUBNUB STATE
--
pubnub_obj = pubnub.new({
    publish_key   = "demo",
    subscribe_key = "demo",
    secret_key    = nil,
    ssl           = nil,
    origin        = "pubsub.pubnub.com"
})

-- 
-- HIDE STATUS BAR
-- 
display.setStatusBar( display.HiddenStatusBar )

-- 
-- CALL PUBLISH FUNCTION
--
function publish(channel, text)
    pubnub_obj:publish({
        channel = channel,
        message = text,
        callback = function(r) --textout(r) 
        end,
        error = function(r) textout(r) 
        end
    })
end

-- 
-- MAIN TEST
-- 
local my_channel = 'lua-dsm'

--
-- Publish String
--
publish("abcd", 'Hello World!' )

--
-- Publish Dictionary Object
--
publish("efgh", { Name = 'John', Age = '25' })

--
-- Publish Array
--
publish("ijkl", { 'Sunday', 'Monday', 'Tuesday' })

      

Post the JavaScript Publishing API Reference on the Docs page.

0


source







All Articles