forked from mayuresh/python-firebase-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_client.py
52 lines (38 loc) · 1.67 KB
/
chat_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from multiprocessing import Process
from firebase import firebase
from sseclient import SSEClient
import json
import time
FIREBASE_URL = "https://dazzling-fire-5952.firebaseio.com/"
def poll_chat():
sse = SSEClient(FIREBASE_URL + "PythonChatDemo/Messages.json")
print("Watching Firebase node - %s" % (FIREBASE_URL + "PythonChatDemo/Messages.json"))
for new_message in sse:
message_data = json.loads(new_message.data)
if message_data is None: # Keep alive
continue
if message_data["data"] is None:
continue
# print("message_data = %s\n" %(message_data))
if message_data["path"] == "/": # Initial Read for old messages
print("Previous messages")
for (nodeid, message) in message_data["data"].items():
#print("message = %s" % (message))
print("%s says: %s" % (message["name"], message["message"]))
else: # New Message
print("%s says: %s" % (message_data["data"]["name"], message_data["data"]["message"]))
# Main
if __name__ == '__main__':
# Start a thread to monitor changes to firebase
t = Process(target=poll_chat)
t.start()
time.sleep(1)
username = raw_input("Input your name: ")
fb = firebase.FirebaseApplication(FIREBASE_URL, None)
# Post initial message to Firebase
fb.post('/PythonChatDemo/Messages', {"name": username, "message": "Joined the chat", ".priority": time.time() * 1000 })
# Post new messages to Firebase
while (True):
message = raw_input("")
print("\n")
fb.post('/PythonChatDemo/Messages', {"name": username, "message": message, ".priority": time.time() * 1000 })