title: Flask-SocketIO: Sending messages back to client
body: <p>I am currently trying to implement a Twitter Stream using Twitter's Streaming API and I am using Flask-SocketIO for use in Python. Now I am able to send an emit to trigger a function in Flask which calls the Twitter API based on some keyword, but I want the text from the stream back to the JavaScript front-end which hasn't been too successful. I am able to send, for instance, a message like "Hi" if I listen on 'message', but I've tried to send JSON back to no avail.</p>
  
  <p>My Flask function for this is:</p>
  
  <pre><code>@socketio.on('my event')
  def handle_my_custom_event(json):
      print('received json: ' + str(json))
      r = api.request('statuses/filter', json)
      for item in r.get_iterator():
          if 'text' in item:
              print(item['text'])
              json_data = {'data': item['text']}
              send(json_data, json=True)
  </code></pre>
  
  <p>JSON in this case is <code>{'track':'$AAPL'}</code> so fetches each tweet about Apple as it happens. <code>item['text']</code> is the string of the tweet text. I've tried using send, also tried using <code>emit('my response',json_data)</code> and listened for 'my response' but I'm not sure I'm doing all of this right.</p>
  
  <p>My front-end script for this currently looks like this:</p>
  
  <pre><code>$(document).ready(function() {
      var socket = io.connect('http://' + document.domain + ':' + location.port);
      socket.on('connect', function() {
          socket.emit('my event', {'track': '$'+data_prices[0]['symbol'].toString()}, function(data){
              console.log(data);
              if (data.error) 
                  console.log('Something went wrong on the server');
  
              if (data.ok)
                  console.log('Event was processed successfully');
          });
      });
  });
  </code></pre>
  
  <p>but I receive no console logs, nothing. </p>
  
  <p>What would I have to do to receive the <code>json_data</code> object in my front-end script and have it log to the console?</p>
  
  <p><strong>UPDATE:</strong> I've managed to get them coming in when I emit them to an event 'my response' but it only seems to log to the console when I shut down the server. Is there a way to keep this running as a stream to the console so I can use the data in the front-end?</p>