how to publish with kwargs?

32 views
Skip to first unread message

ethe...@mail.fcu.edu.tw

unread,
Aug 18, 2014, 6:21:28 AM8/18/14
to autob...@googlegroups.com
when i try to send a payload from example like following:
session.publish('com.myapp.hello', [], { text: 'Hello, world' })

but its not working when i try to show the payload with back-end code:

...
def onhello(msg, kwargs=None, details=None):
           
print("event for 'onhello' received: {0}, {1}, {2}".format(msg, kwargs, details))

sub = yield self.subscribe(onhello, 'com.example.onhello')
...


it works fine with:
session.publish('com.myapp.hello', ['Hello, world!']);

i just want to know how to publish in kwargs way.


ethe...@mail.fcu.edu.tw

unread,
Aug 18, 2014, 2:16:01 PM8/18/14
to autob...@googlegroups.com
okay i figure it out,
the handler should be:

def onhello(*msg, **kwargs):
           
print("event for 'onhello' received: {}, {}".format(msg, kwargs))

 p.s. the topic URI of last post just a copy paste error, not the main problem.

Ethelbert

ethe...@mail.fcu.edu.tw於 2014年8月18日星期一UTC+8下午6時21分28秒寫道:

Tobias Oberstein

unread,
Aug 18, 2014, 3:20:04 PM8/18/14
to autob...@googlegroups.com
Am 18.08.2014 20:16, schrieb ethe...@mail.fcu.edu.tw:
> okay i figure it out,
> the handler should be:
>
> def onhello(*msg,**kwargs):

This Python function is fully generic: it can receive any number of
positional or keywords arguments.

Normally, in your case, you would use:

def onhello(text = None)

Using the fully generic form is not something recommended in general,
since it "hides" the concrete arguments you expect.

You can look at all variants for both JS and Python here:

https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/basic/rpc/arguments



> print("event for 'onhello' received: {}, {}".format(msg,kwargs))
> |
>
> p.s. the topic URI of last post just a copy paste error, not the main
> problem.
>
> Ethelbert
>
> ethe...@mail.fcu.edu.tw於 2014年8月18日星期一UTC+8下午6時21分28秒寫道:
>
> when i try to send a payload from example like following:
>
> |
> session.publish('com.myapp.hello',[],{text:'Hello, world'})
> |
>
> but its not working when i try to show the payload with back-end code:||
>
> |
> ...
> defonhello(msg,kwargs=None,details=None):
> print("event for 'onhello' received: {0}, {1},
> {2}".format(msg,kwargs,details))
>
> sub=yieldself.subscribe(onhello,'com.example.onhello')
> ...
> |
>
>
> it works fine with:
>
> |
> session.publish('com.myapp.hello',['Hello, world!']);
> |
>
> i just want to know how to publish in kwargs way.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Autobahn" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autobahnws+...@googlegroups.com
> <mailto:autobahnws+...@googlegroups.com>.
> To post to this group, send email to autob...@googlegroups.com
> <mailto:autob...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autobahnws/5ef9bcaa-7536-42e5-a269-c5df6b4999a4%40googlegroups.com
> <https://groups.google.com/d/msgid/autobahnws/5ef9bcaa-7536-42e5-a269-c5df6b4999a4%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

ethe...@mail.fcu.edu.tw

unread,
Aug 18, 2014, 8:14:03 PM8/18/14
to autob...@googlegroups.com
Hi Tobias,
I agree with you in all your views.
Now i know how it works.

Have a nice day.
Ethelbert

Tobias Oberstein於 2014年8月19日星期二UTC+8上午3時20分04秒寫道:
Reply all
Reply to author
Forward
0 new messages