php day2013 sports_statistics

33
REAL-TIME SPORTS STATISTICS WITH NODE.JS, WEBSOCKETS AND SYMFONY2

Upload: tobias-josefsson

Post on 17-Jul-2015

585 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Php day2013 sports_statistics

REAL-TIME SPORTS STATISTICSWITH NODE.JS, WEBSOCKETS AND SYMFONY2

Page 2: Php day2013 sports_statistics

#42 TOBIAS JOSEFSSON

Chief Technology Officer”Bobbe” / ”Bobby”@TobiasJosefsson

#8 EMANUEL WINBLAD

Soware Engineer”Manne” / ”Manny”

@ManneW

SPORTS EDITING SWEDENA MEDIA HOUSE FOCUSED ON SPORTS

Page 3: Php day2013 sports_statistics
Page 4: Php day2013 sports_statistics

Overview: Data flow from Operators to Clients

Page 5: Php day2013 sports_statistics

Overview: Data flow from Operators to Clients

Page 6: Php day2013 sports_statistics

Why this complex setup?

”Independent” decoupled parts- Easier to test- Easier to maintain / replace- Each part only does one thing

Multiple languages- There is no silver bullet

Page 7: Php day2013 sports_statistics

Messaging: ZeroMQ

"ZeroMQ is a messaging library, which allows you to design a complex communication system without much effort."

– Nicholas Piël

”Messaging makes everything possible!”– Alvaro Videla, JSDay 2013 (yesterday)

Page 8: Php day2013 sports_statistics

Messaging: ZeroMQ

RabbitMQ ZeroMQBroker Brokerless

Messaging System

Messaging Library

Why:- Lightweight- Minimalistic- Small feature set needed

Page 9: Php day2013 sports_statistics

Messaging: ZeroMQ

Core pattern used:

Page 10: Php day2013 sports_statistics

Overview: Statistics input

Page 11: Php day2013 sports_statistics

Statistics input: External system

Seven Operators per arena, logging events.

- Shots on net, shots wide, shots blocked- Goals and assists- Players currently on the ice, shifts- Penalties- Hits- ...and much more...

Data is transferred using XML files and http(s).

Page 12: Php day2013 sports_statistics

Overview: Parser

Page 13: Php day2013 sports_statistics

Parser: PHP, mongoDBPHP: Familiar, XML processingDoctrine ODM to share documentswith CMS system (Symfony2).

MongoDB: Different attributes/fields depending on event type.

Shot Goal PenaltyPlayer Player Player

Goal section Goal section

Assists

POP, NEP (players on ice)

Penalty reason

Page 14: Php day2013 sports_statistics

Parser: PHP, mongoDB

Possible improvements (when needed):Scale - one parser process per game

(only changes in this component)

Alternatives (PHP based):- Symfony2 (full-stack)- Silex (micro)…

Page 15: Php day2013 sports_statistics

Overview: Editorial input

Page 16: Php day2013 sports_statistics

Editorial input: Symfony2, MySQL

Multiple Operators located in different places

- Live text comments describing the events- Data tagged with games and teams for filtering

- Flexible, adjustable, custom made functionality- CMS also includes REST-ish API used by clients

Page 17: Php day2013 sports_statistics

Overview: Queue Controller

Page 18: Php day2013 sports_statistics

Queue Controller: PHP

Decides what messages that should go where.

Isn’t this a broker!?Yes, kind of…

- Decides which queue(s) to send to.- No information about actual end points.

How?

Page 19: Php day2013 sports_statistics

Queue Controller: PHP// Prepare our context and socket$context = new ZMQContext(); // Subscribe to messages from parser$parser = $context->getSocket(ZMQ::SOCKET_SUB); // Connect to publishing socket$parser->connect('tcp://192.168.0.11:9900');

// Define the channel(s) for// delivery servers to subscribe to$channel = $context->getSocket(ZMQ::SOCKET_PUB); // Bind each channel to an IP and port$channel->bind('tcp://192.168.0.12:5555');

Page 20: Php day2013 sports_statistics

Queue Controller: PHP

// All in an infinite loop// ...// Receive messages from the parser$msg = $parser->recv(); // Which channels will the message go to$recievingChannels = getChannelsForMsg($msg); // Send message to appropriate channelsforeach ($recievingChannels as $channel) {    $channel->send($msg);}

Page 21: Php day2013 sports_statistics

Overview: Delivery

Page 22: Php day2013 sports_statistics

Delivery: Node.js, SockJS

Why Node.JS?- High concurrency- Same language as client

Why SockJS?- Lightweight, but still fallbacks for Web Sockets- Option to get ”raw” sockets

Alternatives:- socket.io (or simply polling...)

http://bit.ly/143An2a

Page 23: Php day2013 sports_statistics

NodeJS server code

var server = sockjs.createServer();    server.on(‘connection’, function(conn) {    conn.on(‘data’, function(msg) {        // Check the action passed        switch (msg.action) {         // ...         // Do the corresponding thing...         // I.e. let the client join a channel         // ...        }    });});

Page 24: Php day2013 sports_statistics

// Game event message received from Queue Controllervar gameEventMessage = ”blablabla”; // Send a message to a clientvar conn = getNextClient(clients); conn.write(gameEventMessage);

NodeJS server code (cont.)

Page 25: Php day2013 sports_statistics

Delivery: Node.js, SockJSSockJS client code

var sockjs = new SockJS('http://192.168.0.13:8080');sockjs.onopen = function() {    // SockJS connection opened}; sockjs.onmessage = function(e) {    // Message recieved    var msg = JSON.parse(e.data);}; sockjs.onclose = function() {  // Connection closed  // Offer the user possibility to reconnect    $('button#reconnect').show();};

Page 26: Php day2013 sports_statistics

Overview: Monitoring and Logging

Page 27: Php day2013 sports_statistics

Overview: Monitoring and Logging

Page 28: Php day2013 sports_statistics

Overview: Clients

Page 29: Php day2013 sports_statistics

Several versions- Web- Mobile version- Mobile version for arenas (using local WiFi)

The different versions share a lot of functionality.

Clients: Symfony2, HTML5, JavaScript

Page 30: Php day2013 sports_statistics

Clients: Desktop version

Page 31: Php day2013 sports_statistics

Clients: Mobile versions

Page 32: Php day2013 sports_statistics

What have we learned?

- Good to keep things separated

- Don’t over engineer the solution!

But......by keeping things separated you can re-engineer later – when needed!

- Use the tools you know how to handle

- Select the most appropriate tool from your own toolchain

Page 33: Php day2013 sports_statistics

SoftwareZeroMQ: http://www.zeromq.orgNodeJS: http://nodejs.orgSockJS: https://github.com/sockjs

ExamplesWeb: http://www.hockeyligan.seMobile: http://m.hockeyligan.se

REFERENCES AND RESOURCES