{"id":15016758,"url":"https://github.com/keinos/mastodon_streamingapi_parser","last_synced_at":"2026-01-04T21:49:34.734Z","repository":{"id":57005359,"uuid":"278989907","full_name":"KEINOS/Mastodon_StreamingAPI_Parser","owner":"KEINOS","description":"✅  PHP7 script that parses the Mastodon Streaming API server-sent events to JSON.","archived":false,"fork":false,"pushed_at":"2020-07-24T17:16:04.000Z","size":98,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-14T01:31:54.877Z","etag":null,"topics":["mastodon","mastodon-api","php","php7","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KEINOS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-12T04:20:04.000Z","updated_at":"2022-02-12T12:29:13.000Z","dependencies_parsed_at":"2022-08-21T12:40:29.225Z","dependency_job_id":null,"html_url":"https://github.com/KEINOS/Mastodon_StreamingAPI_Parser","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FMastodon_StreamingAPI_Parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FMastodon_StreamingAPI_Parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FMastodon_StreamingAPI_Parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FMastodon_StreamingAPI_Parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KEINOS","download_url":"https://codeload.github.com/KEINOS/Mastodon_StreamingAPI_Parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245000450,"owners_count":20545061,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mastodon","mastodon-api","php","php7","php8"],"created_at":"2024-09-24T19:49:20.720Z","updated_at":"2026-01-04T21:49:34.690Z","avatar_url":"https://github.com/KEINOS.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![](https://travis-ci.org/KEINOS/Mastodon_StreamingAPI_Parser.svg?branch=master)](https://travis-ci.org/KEINOS/Mastodon_StreamingAPI_Parser \"View Build Status on Travis\")\n[![](https://img.shields.io/coveralls/github/KEINOS/Mastodon_StreamingAPI_Parser)](https://coveralls.io/github/KEINOS/Mastodon_StreamingAPI_Parser?branch=master \"Code Coverage on COVERALLS\")\n[![](https://img.shields.io/scrutinizer/quality/g/KEINOS/Mastodon_StreamingAPI_Parser/master)](https://scrutinizer-ci.com/g/KEINOS/Mastodon_StreamingAPI_Parser/?branch=master \"Code quality in Scrutinizer\")\n[![](https://img.shields.io/packagist/php-v/keinos/mastodon-streaming-api-parser)](https://github.com/KEINOS/Mastodon_StreamingAPI_Parser/blob/master/.travis.yml \"Version Support\")\n\n# Server-Sent Events parser of Mastodon Streaming API in PHP\n\nThis class simply parses the received lines from [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) of the [Mastodon Streaming API](https://docs.joinmastodon.org/methods/timelines/streaming/) to JSON object string.\n\nThe below lines (server-sent event messages) will be parsed in JSON as below.\n\n```text\n5b1\nevent: update\ndata: {\"id\":\"10000 ... visible\\\"\u003e0FS8QET2TT\u003c/span\u003e\u003c/a\u003e\u003cbr\u003e\u003ca hr\n350\nef=\\\"https://www ... ts\":[],\"mentions\":[],\"tags\":[],\"emojis\":[]}\n```\n\n↓\n\n```php\nuse \\KEINOS\\MSTDN_TOOLS\\Parser\\Parser;\n\n$parser = new Parser();\n\nwhile (! feof($stream)) {\n    $line = fgets($stream);\n    $json = $parser-\u003eparse($line);\n    if (false === $json) {\n        continue;\n    }\n    echo $json . PHP_EOL;\n}\n```\n\n↓\n\n```json\n{\"event\":\"update\",\"payload\":{\"id\":\"10000 ... visible\\\"\u003e0FS8QET2TT\u003c\\/span\u003e\u003c\\/a\u003e\u003cbr\u003e\u003ca href=\\\"https:\\/\\/www .... ts\":[],\"mentions\":[],\"tags\":[],\"emojis\":[]}}\n```\n\nUse this class if you are receiving the streaming signal directly from Mastodon Streaming API via socket connection, rather than WebSocket which requires an access token when upgrading the protocol.\n\n## Install\n\n```bash\ncomposer require keinos/mastodon-streaming-api-parser\n```\n\n## Usage\n\n- Instantiation\n\n    ```php\n    $parser = new \\KEINOS\\MSTDN_TOOLS\\Parser\\Parser();\n    ```\n\n- Method\n\n    ```php\n    /**\n     * @param  string $line   Received streaming line.\n     * @return bool|string    Returns the data unit in JSON string. Or false if the\n     *                        status is \"buffering in progress\".\n     */\n    $json = $parser-\u003eparse($line);\n    ```\n\n- Returned JSON string structure\n\n    ```json\n    {\n        \"event\":\"[Event name]\",\n        \"payload\":\"[Data of the event]\"\n    }\n    ```\n\n- Interface: [ParserInterface.php](https://github.com/KEINOS/Mastodon_StreamingAPI_Parser/blob/master/src/interfaces/ParserInterface.php)\n\n### Sample\n\n```php\n// Instantiate the parser\n$parser = new \\KEINOS\\MSTDN_TOOLS\\Parser\\Parser();\n// Open socket\n$fp = fsockopen($hostname, $port, $errno, $errstr, $timeout);\n// Send GET request\nfwrite($fp, $req);\n// Looper\nwhile (! feof($fp)) {\n    // Read the stream\n    $read = fgets($fp);\n    // Buffer each line until it returns the data\n    $json = $parser-\u003eparse($read);\n    if (false === $json) {\n        continue;\n    }\n    // Do something with the data\n    echo $json . PHP_EOL;\n}\n// Close connection\nfclose($fp);\n\n```\n\n- Complete sample see: [./samples/Main.php](https://github.com/KEINOS/Mastodon_StreamingAPI_Parser/blob/master/samples/Main.php)\n\n## Specifications/Info\n\n- PHP: ^7.1 || ^8.0\n- Available [event types](https://docs.joinmastodon.org/methods/timelines/streaming/#event-types-a-idevent-typesa) to detect: `update` and `delete`\n- Source Code: [Mastodon_StreamingAPI_Parser](https://github.com/KEINOS/Mastodon_StreamingAPI_Parser) @ GitHub\n- Package release: [keinos/mastodon-streaming-api-parser](https://packagist.org/packages/keinos/mastodon-streaming-api-parser) @ Packagist\n\n## Contribution\n\n- Issues\n  - Throw an issue at: https://github.com/KEINOS/Mastodon_StreamingAPI_Parser/issues\n  - If the issue is a bug, then please provide a simple example that are reproducible.\n  - If the issue is a feature request, then also provide a simple example with the results you wish, so we can create a test before implementing your feature.\n- PR (Pull Request)\n  - Please **PR to the `padawan` branch**. After the CI tests/reviews, then merged to `master` branch it will ;-)\n  - For more see: [CONTRIBUTE.md](https://github.com/KEINOS/Mastodon_StreamingAPI_Parser/blob/master/CONTRIBUTE.md)\n- Rules\n  - Be nice, be happy and stay safe.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeinos%2Fmastodon_streamingapi_parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeinos%2Fmastodon_streamingapi_parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeinos%2Fmastodon_streamingapi_parser/lists"}