Skip to content

Instantly share code, notes, and snippets.

@kcowgill
Created May 31, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcowgill/b823953bfc74b80057afc3d3ca786aed to your computer and use it in GitHub Desktop.
Save kcowgill/b823953bfc74b80057afc3d3ca786aed to your computer and use it in GitHub Desktop.
# DDPClient from here: https://github.com/hharnisc/python-ddp
from DDPClient import DDPClient
import hashlib
import time
# these are callbacks
def show_data(data, *more):
print("Data received: '{}':'{}'".format(data, more))
def received_message(data):
print("MESSAGE received: '{}'".format(data))
def connected():
print('* Hey there good lookin, whatcha got cookin\'?')
def closed(code, reason):
print('* CONNECTION CLOSED {}:{}'.format(code,reason))
def failed(collection, data):
print('* FAILED - data: {}'.format(str(data)))
def subscribe(data, more):
print('* subscribe data: {}:{}'.format(str(data),more))
server_url = 'ws://your.server.here:3000/websocket'
client = DDPClient(server_url, auto_reconnect=True, auto_reconnect_timeout=.5, debug=True)
client.on('connected', connected)
client.on('reconnected', connected)
client.on('socket_closed', closed)
client.on('failed', failed)
client.connect()
# I don't yet know how to look these up programatically
username = "username"
user_id = "SomeUserID"
room_id = "SomeRoomID"
passw = "SomePassword"
hashedPass = hashlib.sha256(passw.encode('utf-8'))
hashDigest = hashedPass.hexdigest()
ret = client.call('login', [{ "user": {"username": username},
"password": { "digest": hashDigest, "algorithm":"sha-256" } }], show_data)
# this lets your program receive direct messages
client.subscribe('stream-notify-user', ["{}/notification".format(user_id), False], subscribe)
# this lets your program receive messages from any room it's in
client.subscribe('stream-room-messages', [room_id, False], subscribe)
client.call('joinRoom', [room_id], show_data
client.call('sendMessage', [{"rid":room_id, "msg":'hi there!'}], show_data)
while True:
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment