3 * Laconica, the distributed open-source microblogging tool
5 * Plugin to do "real time" updates using Orbited + STOMP
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@controlyourself.ca>
25 * @copyright 2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
34 require_once INSTALLDIR.'/plugins/Realtime/RealtimePlugin.php';
37 * Plugin to do realtime updates using Orbited + STOMP
39 * This plugin pushes data to a STOMP server which is then served to the
40 * browser by the Orbited server.
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://laconi.ca/
49 class OrbitedPlugin extends RealtimePlugin
51 public $webserver = null;
52 public $webport = null;
53 public $channelbase = null;
54 public $stompserver = null;
55 public $stompport = null;
56 public $username = null;
57 public $password = null;
58 public $webuser = null;
59 public $webpass = null;
61 protected $con = null;
63 function onStartShowHeadElements($action)
65 // See http://orbited.org/wiki/Deployment#Cross-SubdomainDeployment
66 $action->element('script', null, ' document.domain = document.domain; ');
69 function _getScripts()
71 $scripts = parent::_getScripts();
73 $port = (is_null($this->webport)) ? 8000 : $this->webport;
75 $server = (is_null($this->webserver)) ? common_config('site', 'server') : $this->webserver;
77 $root = 'http://'.$server.(($port == 80) ? '':':'.$port);
79 $scripts[] = $root.'/static/Orbited.js';
80 $scripts[] = common_path('plugins/Orbited/orbitedextra.js');
81 $scripts[] = $root.'/static/protocols/stomp/stomp.js';
82 $scripts[] = common_path('plugins/Orbited/orbitedupdater.js');
87 function _updateInitialize($timeline, $user_id)
89 $script = parent::_updateInitialize($timeline, $user_id);
91 $server = $this->_getStompServer();
92 $port = $this->_getStompPort();
94 return $script." OrbitedUpdater.init(\"$server\", $port, ".
95 "\"{$timeline}\", \"{$this->webuser}\", \"{$this->webpass}\");";
100 require_once(INSTALLDIR.'/extlib/Stomp.php');
102 $url = $this->_getStompUrl();
104 $this->con = new Stomp($url);
106 if ($this->con->connect($this->username, $this->password)) {
107 $this->log(LOG_INFO, "Connected.");
109 $this->log(LOG_ERR, 'Failed to connect to queue server');
110 throw new ServerException('Failed to connect to queue server');
114 function _publish($channel, $message)
116 $result = $this->con->send($channel,
117 json_encode($message));
120 // TODO: parse and deal with result
123 function _disconnect()
125 $this->con->disconnect();
128 function _pathToChannel($path)
130 if (!empty($this->channelbase)) {
131 array_unshift($path, $this->channelbase);
133 return '/' . implode('/', $path);
136 function _getStompServer()
138 return (!is_null($this->stompserver)) ? $this->stompserver :
139 (!is_null($this->webserver)) ? $this->webserver :
140 common_config('site', 'server');
143 function _getStompPort()
145 return (!is_null($this->stompport)) ? $this->stompport : 61613;
148 function _getStompUrl()
150 $server = $this->_getStompServer();
151 $port = $this->_getStompPort();
152 return "tcp://$server:$port/";