igorw / EventSource

A PHP 5.3 library for creating an EventSource stream.
MIT License
105 stars 8 forks source link

Add Support for JSON Data #3

Closed dator-zz closed 12 years ago

dator-zz commented 12 years ago

What about adding the support to send json data ?

AFAIK, we can only send text data, it can be very useful to be able to send json data to the client ?

http://www.html5rocks.com/en/tutorials/eventsource/basics/#toc-json-data

What do you think ?

igorw commented 12 years ago

This is not something the library should handle. It simply implements the EventSource protocol. What kind of messages you send are application-specific. It's really trivial to do:

<?php

$data = array('userIds' => array(21, 43, 127));

$stream
    ->event()
        ->setData(json_encode($data));
    ->end()
    ->flush();
var stream = new EventSource('stream.php');

stream.addEventListener('message', function (event) {
    var data = JSON.parse(event.data);
    console.log('User IDs: '+data.userIds.join(', '));
});

I will add some docs for this to the README though.

dator-zz commented 12 years ago

hum, ok it's too late for me !

Thanks a lot ;)

igorw commented 12 years ago

I've added this to the docs.