Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import socket
import uuid
import random
import threading
import time
import random
import math
HOST = 'pubsub.pubnub.com'
PORT = 80
numID = random.randrange(5000, 10000)
print 'creating', numID, ' UUIDs'
id = []
for x in range(0, numID-1):
id.append(uuid.uuid4())
print 'We will send a random quantity of messages every 10 seconds, each contains a randomly selected UUID.'
def socketPool(message):
#TODO: Implement socketPool w/ size 100
def handleSocketRead(s):
while True:
try:
msg = s.recv(4096)
except socket.timeout, e:
err = e.args[0]
print 'recv timed out, done reading'
break
except socket.error, e:
print e
break
else:
continue
#print repr(msg)
def send():
time.sleep(10)
threading.Thread(target=send).start()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostbyname(HOST), PORT))
s.settimeout(2)
numMessages = random.randrange(math.floor(0.5*numID), 2*numID)
print 'Sending '+ str(numMessages) +' messages.'
time_start = time.time()
tmr = threading.Thread(target=handleSocketRead, args=[s])
tmr.start()
for message in range(0, numMessages):
#print 'we are on message', message+1, 'of', numMessages
s.send('GET /publish/pub-c-5afaf11d-aa91-4a40-b0d2-77961fb3a258/sub-c-0cd3a376-28ac-11e4-95a7-02ee2ddab7fe/0/HyperLogLogDemo1/0/"'+str(id[random.randrange(0, numID-1)])+'" HTTP/1.1\r\nHost: pubsub.pubnub.com\r\n\r\n')
print 'It took', str(time.time()-time_start), 'runtime to send', numMessages, 'messages'
handleSocketRead(s)
s.close()
send()

PubNub HTTP Pipelining

Sending messages in parallel and in a Single TCP Packet (one Ethernet Frame) is really easy with PubNub HTTP Pipelining! We provide an example below of the Socket level data you send per message. Note that we support the full Pipelining mechanism for HTTP Pipelining. You may send serially on a single socket without waiting for the response and the responses return in order.

Essentially -- To really make this happen with top tier performance you would follow this recipe:

  1. Open 100 TCP sockets to geo1.pubnub.com (closest data center) with fallbacks next closest data center of geo2.pubnub.com geo3.pubnub.com.
  2. Send HTTP/1.1 commands round robin -- without waiting for responses you may continue sending commands! -- Pipelining!
  3. Add socket fail detect or TIMEOUTs as needed for retries.
  4. Rejoice with awesome cool-guy dance moves.

PubNub Publish Format

GET /publish/PUB_KEY/SUB_KEY/0/CHANNEL/0/PAYLOAD HTTP/1.1\r\n 
Host: pubsub.pubnub.com\r\n\r\n

Pipeline Publish 3 Messages in 1 TCP Packet

GET /publish/demo/demo/0/chan/0/"ONE" HTTP/1.1\r\n 
Host: pubsub.pubnub.com\r\n\r\n
GET /publish/demo/demo/0/chan/0/"TWO" HTTP/1.1\r\n 
Host: pubsub.pubnub.com\r\n\r\n
GET /publish/demo/demo/0/chan/0/"THREE" HTTP/1.1\r\n 
Host: pubsub.pubnub.com\r\n\r\n

This following example will show you what publishing three messages in a single TCP Packet may look like. You should test this directly in your Terminal Window via TELNET app.

telnet pubsub.pubnub.com 80

GET /publish/demo/demo/0/my_channel/0/"ONE" HTTP/1.1
Host: pubsub.pubnub.com

GET /publish/demo/demo/0/my_channel/0/"TWO" HTTP/1.1
Host: pubsub.pubnub.com

GET /publish/demo/demo/0/my_channel/0/"THREE" HTTP/1.1
Host: pubsub.pubnub.com

PubNub HTTP Streaming

To subscribe and receive messages, issue a GET request in the following format; we'll use Telnet as an example.

Stream All Messages

telnet pubsub.pubnub.com 80

GET /stream/demo/bot/0/10000 HTTP/1.1
Host: pubsub.pubnub.com

Note the socket will remain open as it receives data and even during silence. You'll need to re-send the GET upon a chunk complete event.

Use Cases

  • Server Side
  • Stock Streaming
  • IoT Internet Of Things
  • Voice and Video
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.