Python server push solution: orbited + rabbitmq

Source: Internet
Author: User
Tags stomp rabbitmq
Source: http://simple-is-better.com/

Recently, the company needs to use server push technology. Google, nodejs is good, but the company's things are made in Python,

So I chose orbited + rabbitmq for python, but the orbited document is very lacking. So I need to take a note.

The following are all tests performed on Windows. Principle:

Orbited installation:

PIP install twisted orbited stomp. py

Rabbitmq: first install Erlang and then install the win version EXE file of rabbitmq. Because the stomp protocol is used,

So there are two plug-in amqp_client-2.5.1.ez, rabbitmq_stomp-2.5.1.ez,

Go to the rabbitmq \ plugins directory, and put a rabbitmq. config file in c: \ Documents and Settings \ CZ. Chen \ Application Data \ rabbitmq. The content is

[
{Rabbitmq_stomp, [{tcp_listeners, [{"127.0.0.1", 61613}]}
].

Enable these two plug-ins to start the rabbitmq service.

Next I will go back to Python. Tg2 is used in the framework. I have created a project named comet and assigned a chat. ini to comet to configure orbited.

[Listen]
# This is the server which provides the socket-proxy for Javascript

Http: // 9000

# The following enables the morbidq stomp Message Queue
# Stomp: // 61613

[Access]
# Allow incoming HTTP requests on port 9000 to connect
# Localhost: 61613 (I. e. the morbidq stomp server)
# The * refers to
# *-> Localhost: 61613
*-> Localhost: 61613

[Global]
Session. ping_interval = 300

Then add the code in controllers \ Root. py. I have omitted some code, the code is as follows:

#-*-Coding: UTF-8 -*-
"Main controller """
From TG import expose, Flash, require, URL, request, redirect, response
From pylons. i18n import ugettext AS _, lazy_ugettext as L _
From tgext. admin. tgadminconfig import tgadminconfig
From tgext. admin. Controller import admincontroller
From repoze. What import predicates

From Comet. Lib. Base import basecontroller
From Comet. Model import dbsession, metadata
From Comet import Model
From Comet. controllers. Secure import securecontroller

From Comet. controllers. Error import errorcontroller

Import stomp
Import JSON

### Rabbitmq
Conn = stomp. Connection ([('0. 0.0.0 ', 61613), ('2014. 0.0.1', 127)], 'Guest ', 'Guest ')
Print Conn
Conn. Start ()
Conn. Connect (wait = true)
Conn. subscribe (Destination = '/topic/RFID', ACK = 'auto ')
###########
_ All _ = ['rootcontroller']

Class rootcontroller (basecontroller ):

@ Expose ('comet. Templates. chat ')
Def chat (Self ):
"Handle the front-page ."""
Return {}

@ Expose ('json ')
Def add_msg (self, ** kW ):
Print kw
Conn. Send (
JSON. dumps ({'msg ': kW. Get ('msg ','')}),
Destination = '/topic/RFID ')
Return {}

Replace copy in the static directory under the orbited package with orbited in the comet \ public directory of the comet project,

Add another template: chat. Mak in templates. The Code is as follows:

<SCRIPT type = "text/JavaScript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/orbited/JSON. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/orbited. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
// This line is required to allow our chat server and this
// Page to operate on different ports...
Document. Domain = Document. domain;
// Establish the port and server for the orbited Server
Orbited. settings. Port = 9000;
Orbited. settings. hostname = "127.0.0.1 ";

Orbited. loggers ['orbited. tcpsocket ']. Enabled = true;
Orbited. settings. log = true
// Enable streaming operation
Orbited. settings. Streaming = true;
// This object is referenced by stomp. js
Tcpsocket = orbited. tcpsocket;
</SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/orbited/protocols/stomp. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
VaR add_message = function (text ){
VaR node = $ ('<Div class = "chat-message"> </div> ');
Node. append (Text ['msg ']);
$ ('. Chat-track'). append (node );
};
$ (Document). Ready (function (){
Stomp = new stompclient ();
Stomp. onconnectedframe = function (FRAME ){
Stomp. subscribe ("/topic/RFID ");
};
Stomp. onmessageframe = function (FRAME ){
Add_message (JSON. parse (frame. Body ));
// Add_message (frame. Body );
};
Stomp. Connect ('localhost', 61613, 'Guest ', 'Guest ');
$ ('. Chat-entry. Chat-trigger'). Click (function (){
VaR chatter = $ ('. Chat-entry. Chatter ');
VaR value = chatter. ATTR ('value ');
If (value. Length ){
// Stomp. Send (value, "/messages ");
$. Post ("/add_msg", {"MSG": Value });
Chatter. ATTR ('value ','');
}
});
});
</SCRIPT>

<Div id = "chat">
<H2> real-time chat </H2>
<Div class = "chat-trace">
</Div>
<Div class = "chat-entry">
Chat:
<Input class = "chatter"/>
<Button class = "chat-Trigger"> send </button>
</Div>
</Div>

Switch to the comet project directory and run orbited -- Config = chat. ini to start orbited,

Then run Paster serve -- reload development. ini to start Tg2,

Then open two pages in the browser: http: // 127.0.0.1: 8080/chat, and you can chat in real time...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.