How to integrate SockJS with another web map

As an alternative to Socket.io, there is SockJS ( https://github.com/sockjs/sockjs-client ), which seems to be better supported and more stable than Socket.io.

This question concerns a conceptual understanding of the architecture of using SockJS with a web framework, say, to create a chat application

Currently, I understand that you need a SockJS client and a SocketJS server (in my case, I intend to use SockJS-Tornado ( https://github.com/MrJoes/sockjs-tornado )) to be able to create websockets style messages .

But how does SockJS (SockJS client + SockJS-Tornado) integrate with a web map that does the rest of the work (for example, serves a page, writes / reads to / from db, etc.). For example, how does the SockJS-Tornado component communicate with the structure web server? In particular, any direction of this work with web2py (python web map) is much appreciated.

+7
source share
3 answers

You are right, for SockJS you need a server with sockjs support and a JavaScript client library in your browser.

Typically, there are two integration patterns, let's say you want to use sockjs tornadoes:

  • You can have your entire site from Tornado. At the same time, intercept the sockjs tornado on some path, for example, "http://mysite.com/sockjs". In this case, both sites and sockjs will be served from the mysite.com domain.
  • You can save your site in any language / framework that he wrote, and add sockjs-serveras to another componenttent in another domain, for example. 'Http://sockjs.mysite.com/sockjs'.

In addition, you can use any options for this - for example: have two servers inside, but expose them as one domain using an intelligent loadblancer (for example, haproxy).

+4
source

If you use web2py as a framework, you can see comet_messaging.py in gluon / contrib. It provides a function (comet_send) to receive messages from the web2py application for your clients on the network. It is based on a tornado (without SockJS support), but DistributeHandler can subclass a SockJS connection to provide backup transport support. With this approach, your clients send messages through typical GET or POST HTTP requests, which are processed using web2py controllers (or other frameworks) and receive messages from web2py controllers calling comet_messaging.comet_send (), which sends an email request to the tornado instance, which then his list of listeners explodes.

The modified lines in comet_messaging look like this: (The notification notification becomes on_open):

class DistributeHandler(sockjs.tornado.SockJSConnection): def on_open(self, r): group,token,name = [None, None, None] self.group = group or 'default' self.token = token or 'none' self.name = name or 'anonymous' 

and

 urls=[ (r'/', PostHandler), (r'/token', TokenHandler), (r'/realtime', DistributeHandler)] 

Note: I had to remove the regex group in the DistributeHandler URL, as the sockJS tornado was choking. Still trying to figure out how to get the parameters from the path to the on_open handler.

+1
source

This gives a complete answer on how to integrate SockJS into Django: stack overflow

Basically you need:

  • Tornado + SockJS-Tornado
  • Redis + brukva

I use this configuration in my own project and it works very well.

Or: you are trying to use the autobahn path: http://autobahn.ws/ (I have not tried it yet)

0
source

All Articles