title: What is the difference between using ZMQ PUB with .connect() or .bind() methods?
body: <p>In Python ZMQ <strong>publisher/subscriber</strong> sample template, the publisher uses <strong><code>.bind()</code></strong> method and the subscriber uses <code>.connect()</code> method, that connected to the bind IP address.</p>
  
  <p>But we can replace <code>.bind()</code> and <code>.connect()</code> each with the other.  </p>
  
  <p>My question is that what is the difference between two cases that determined below?<br>
  (two scripts in these cases work fine)</p>
  
  <p>The first case, as default:</p>
  
  <p><strong>pub1.py:</strong></p>
  
  <pre><code>import zmq
  import time
  from datetime import datetime
  
  def create_pub_socket():
      context = zmq.Context()
      socket = context.socket(zmq.PUB)
      socket.bind("tcp://127.0.0.1:9002")  # notice
      return socket
  
  def publish(pub_socket):
      message = {
          'data': 'hi my name is benyamin',
          'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
      }
      pub_socket.send_json(message, 0)
      return message
  
  if __name__ == '__main__':
      socket = create_pub_socket()
  
      while True:
          print('\n')
          print('publisher: ', publish(socket))
          time.sleep(1)
  </code></pre>
  
  <p><strong>sub1.py:</strong></p>
  
  <pre><code>import zmq
  
  if __name__ == '__main__':
      context = zmq.Context()
      socket = context.socket(zmq.SUB)
      socket.setsockopt(zmq.SUBSCRIBE, "")
      socket.connect("tcp://127.0.0.1:9002")  # notice
  
      while True:
          data = socket.recv_json()
          print('subscriber: ', data)
          print('\n')
  </code></pre>
  
  <p>And the second case, as the modified setup, that reversed the use of the <code>.connect()</code> and <code>.bind()</code> methods:</p>
  
  <p><strong>pub2.py:</strong></p>
  
  <pre><code>import zmq
  import time
  from datetime import datetime
  
  def create_pub_socket():
      context = zmq.Context()
      socket = context.socket(zmq.PUB)
      socket.connect("tcp://127.0.0.1:9002")  # notice
      return socket
  
  def publish(pub_socket):
      message = {
          'data': 'hi my name is benyamin',
          'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
      }
      pub_socket.send_json(message, 0)
      return message
  
  if __name__ == '__main__':
      socket = create_pub_socket()
  
      while True:
          print('\n')
          print('publisher: ', publish(socket))
          time.sleep(1)
  </code></pre>
  
  <p><strong>sub2.py:</strong></p>
  
  <pre><code>import zmq
  
  if __name__ == '__main__':
      context = zmq.Context()
      socket = context.socket(zmq.SUB)
      socket.setsockopt(zmq.SUBSCRIBE, "")
      socket.bind("tcp://127.0.0.1:9002")  # notice
  
      while True:
          data = socket.recv_json()
          print('second subscriber: ', data)
          print('\n')
  </code></pre>