WAMP Server Application with Topic Publishing

  • Share this:

Code introduction


This code creates a WAMP (Wolfram Advanced Messaging Protocol) server application using the Autobahn library. The application publishes a message to a topic named 'my.topic' with two arguments after joining.


Technology Stack : Autobahn, WampProtocol, ApplicationSession, TCP4ServerEndpoint, asyncio, twisted

Code Type : WAMP (Wolfram Advanced Messaging Protocol) server application

Code Difficulty : Intermediate


                
                    
import asyncio
from autobahn.wamp import ApplicationSession, WampProtocol
from twisted.internet.endpoints import TCP4ServerEndpoint

def xxx(arg1, arg2):
    # Define a WAMP application session
    class MyApplicationSession(ApplicationSession):
        def on_join(self):
            # Publish a message to a topic
            self.publish(u'my.topic', arg1, arg2)

    # Create a WAMP application session object
    session = MyApplicationSession()

    # Run the WAMP application
    loop = asyncio.get_event_loop()
    endpoint = TCP4ServerEndpoint(0, "localhost")
    protocol = WampProtocol(session)
    transport, protocol = endpoint.listen(protocol)
    loop.run_until_complete(loop.create_server(protocol, host="localhost", port=8080))
    loop.run_forever()