Skip to content

Instantly share code, notes, and snippets.

@nickfishman
Created October 15, 2013 22:13
Show Gist options
  • Save nickfishman/6999499 to your computer and use it in GitHub Desktop.
Save nickfishman/6999499 to your computer and use it in GitHub Desktop.
End-to-end example of autobahn.ws with dynamic pubsub channels. Each channel is private, even though it matches the same prefix URI defined in the server. Based on example in https://github.com/tavendo/AutobahnPython/tree/master/examples/wamp/pubsub/simple/example1.
import os, sys
from twisted.python import log
from twisted.internet import reactor
from autobahn.websocket import connectWS
from autobahn.wamp import WampClientFactory, \
WampClientProtocol
ROOM_ID = ''
BASE_URI = "http://example.com/rooms/"
class PubSubClient1(WampClientProtocol):
def onSessionOpen(self):
print "Subscribing to %s" % (BASE_URI + ROOM_ID)
self.subscribe(BASE_URI + ROOM_ID, self.onEvent)
self.sendEvent()
def onEvent(self, topicUri, event):
print "Event", topicUri, event
def sendEvent(self):
message = "Hello %s! (%s)" % (ROOM_ID, os.getpid())
self.publish(BASE_URI + ROOM_ID, message)
reactor.callLater(2, self.sendEvent)
if __name__ == '__main__':
ROOM_ID = sys.argv[1]
print "I am room %s, pid %s" % (ROOM_ID, os.getpid())
log.startLogging(sys.stdout)
debug = len(sys.argv) > 1 and sys.argv[1] == 'debug'
factory = WampClientFactory("ws://localhost:9000", debugWamp = debug)
factory.protocol = PubSubClient1
connectWS(factory)
reactor.run()
<!DOCTYPE html>
<html>
<head>
<!-- include AutobahnJS .. that's all you need -->
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
// WAMP session object
var sess = null;
window.onload = function() {
var wsuri;
if (window.location.protocol === "file:") {
wsuri = "ws://localhost:9000";
} else {
wsuri = "ws://" + window.location.hostname + ":9000";
}
// connect to WAMP server
ab.connect(wsuri,
// WAMP session was established
function (session) {
sess = session;
console.log("Connected to " + wsuri);
test();
},
// WAMP session is gone
function (code, reason) {
sess = null;
if (code == ab.CONNECTION_UNSUPPORTED) {
window.location = "http://autobahn.ws/unsupportedbrowser";
} else {
console.log(reason);
}
}
);
};
function test() {
sess.subscribe("http://example.com/rooms/XYZ", onEvent);
}
function onEvent(topicUri, event) {
console.log(topicUri);
console.log(event);
}
function publishEvent() {
sess.publish("http://example.com/rooms/XYZ", "Hello XYZ! (" + navigator.userAgent + ")");
}
</script>
</head>
<body>
<h1>PubSub with AutobahnJS - Example 1</h1>
<noscript>
<span style="color: #f00; font-weight: bold;">
You need to turn on JavaScript.
</span>
</noscript>
<button onclick="publishEvent()">Publish</button>
<p>
Open development console (press F12) to watch.
</p>
</body>
</html>
import sys
from twisted.python import log
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File
from autobahn.websocket import listenWS
from autobahn.wamp import WampServerFactory, \
WampServerProtocol
BASE_URI = "http://example.com/rooms"
class PubSubServer1(WampServerProtocol):
def onSessionOpen(self):
self.registerForPubSub(BASE_URI, True)
if __name__ == '__main__':
log.startLogging(sys.stdout)
debug = len(sys.argv) > 1 and sys.argv[1] == 'debug'
factory = WampServerFactory("ws://localhost:9000", debugWamp = debug)
factory.protocol = PubSubServer1
factory.setProtocolOptions(allowHixie76 = True)
listenWS(factory)
webdir = File(".")
web = Site(webdir)
reactor.listenTCP(8080, web)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment