New to the group + How to call protocol methods from another module

#1

Hello guys,

I am new to autobahn and just joined this group.

I am kind of new to Python too so please forgive my mistakes.

I’d like to implement websockets in an application but I am kind of stuck at the moment.

I would like to call protocol methods, such as sending a specific message, from another module.

Not complicated, well maybe, here is how my application works :

A process is running outside the protocol module and is supposed to retrieve the protocol instance every X period in order to see if a connection has opened.

When a user opens a connection, it fires the onOpen() functions, which sets the value of the attribute isRunning of the protocol instance to True.

As long as the isRunning attribute of the protocol instance is set to True, the process fires a specific function of the protocol and sends a message.

The Protocol inherits from WebSocketServerProtocol and when it’s initiated sets the value of isRunning to False.

So I used the Singleton pattern with a decorator for the protocol in order to retrieve the right instance from the other module.

But, when a connection is opened, the value of the attribute is set once to True (in onOpen() ) but then is immediatly reseted to False.

Somehow i think it’s because when it retrieves the protocol instance, its method init which sets the attribute again to False is called.

How come ? Should i use Borg Pattern in order to set correctly the attribute ?

Thanks a lot for your great work on Autobahn !

Olivier

···

Here is the code of the protocol :

def singleton(cls):

instance = cls()

instance.call = lambda: instance

return instance

@singleton

class ServerProtocol(WebSocketServerProtocol):

def init(self) :

print “Init”

self.isRunning = False

def myFonc(self):

self.sendMessage(myMessage)

def onOpen(self):

self.isRunning = True

print “new connection”

And Here is how i retrieve the protocol in the other module.

I am using this function every X secs:

PeriodicFunc() :

server = ServerProtocol()

if server.isRunning :

server.myFonc()

0 Likes

#2

Hi Olivier,

I'm not sure I fully grapsh what you are trying to achieve, but here is a complete example

https://github.com/tavendo/AutobahnPython/blob/master/examples/websocket/broadcast/server.py

of a WebSocket server where client WebSocket sessions are tracked, and the server broadcasts messages to all _currently_ connected session periodically.

Maybe you can take the code as a starting point to get what you want?

Cheers,
Tobias

Hello guys,
I am new to autobahn and just joined this group.
I am kind of new to Python too so please forgive my mistakes.

I'd like to implement websockets in an application but I am kind of
stuck at the moment.
I would like to call protocol methods, such as sending a specific
message, from another module.

Not complicated, well maybe, here is how my application works :

A process is running outside the protocol module and is supposed to
retrieve the protocol instance every X period in order to see if a
connection has opened.

A server can accept more than 1 connection .. so there will be many WebSocketServerProtocol instances ..

···

Am 26.10.2012 18:01, schrieb Olivier:

When a user opens a connection, it fires the onOpen() functions, which
sets the value of the attribute isRunning of the protocol instance to True.
As long as the isRunning attribute of the protocol instance is set to
True, the process fires a specific function of the protocol and sends a
message.
The Protocol inherits from WebSocketServerProtocol and when it's
initiated sets the value of isRunning to False.

So I used the Singleton pattern with a decorator for the protocol in
order to retrieve the right instance from the other module.
But, when a connection is opened, the value of the attribute is set once
to True (in onOpen() ) but then is immediatly reseted to False.
Somehow i think it's because when it retrieves the protocol instance,
its method __init__ which sets the attribute again to False is called.

How come ? Should i use Borg Pattern in order to set correctly the
attribute ?

Thanks a lot for your great work on Autobahn !

Olivier
_______________________________________

Here is the code of the protocol :

def singleton(cls):
     instance = cls()
     instance.__call__ = lambda: instance
     return instance

@singleton
class ServerProtocol(WebSocketServerProtocol):
    def __init__(self) :
       print "Init"
       self.isRunning = False
    def myFonc(self):
          self.sendMessage(myMessage)

    def onOpen(self):
       self.isRunning = True
       print "new connection"

And Here is how i retrieve the protocol in the other module.
I am using this function every X secs:
PeriodicFunc() :
          server = ServerProtocol()
          if server.isRunning :
             server.myFonc()

0 Likes

#3

Hello Tobias,

thanks a lot for your quick answer, i am currently trying to reimagine my application structure due to your answer.

It leads me to another question, which is :
Is there a way to order a protocol to listen to a specific event outside of the WebSocketServerProtocol ?
For instance, Server side, i got my server running and another application, can i order my protocol to listen to this application events ?

Thanks a lot again.

Olivier

0 Likes

#4

Hello Tobias,

thanks a lot for your quick answer, i am currently trying to reimagine
my application structure due to your answer.

It leads me to another question, which is :
Is there a way to order a protocol to listen to a specific event outside
of the WebSocketServerProtocol ?

It's working like this: a WAMP client orders a WAMP server (broker) to listen for events on some topic (= subscribe).

When the broker receives an event on some topic from a client (publish),
it dispatches the event to all subscribers.

You can however also dispatch events on broker without a client having published the event. I.e. you can dispatch events is response to a procedure called via RPC, or in response to something completely different.

For instance, Server side, i got my server running and another
application, can i order my protocol to listen to this application events ?

"another application": is that within the same server process?

If so it's easy to do dispatch of events on topics originating not from other clients, but something else by using

WampServerFactory.dispatch

If it's originating outside (in a different process), you need a mechanism to connect both processes.

You can do that again via WAMP, or you can (if possible), spawn the other process from your main server by using Twisted's processProtocol,
communicate via Pipes, and then do the dispatching.

···

Am 31.10.2012 14:02, schrieb Olivier:

Thanks a lot again.

Olivier

0 Likes