Skip to main content

SockJS server implementation for aiohttp.

Project description

https://travis-ci.com/aio-libs/sockjs.svg?branch=master

sockjs is a SockJS integration for aiohttp. SockJS interface is implemented as a aiohttp route. Its possible to create any number of different sockjs routes, ie /sockjs/* or /mycustom-sockjs/*. You can provide different session implementation and management for each sockjs route.

Simple aiohttp web server is required:

[server:main]
use = egg:gunicorn#main
host = 0.0.0.0
port = 8080
worker = aiohttp.worker.GunicornWebWorker

Example of sockjs route:

def main(global_settings, **settings):
    app = web.Application()
    app.router.add_route('GET', '/', index)
    sockjs.add_endpoint(app, prefix='/sockjs', handler=chatSession)
    web.run_app(app)

Client side code:

<script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>
<script>
  var sock = new SockJS('http://localhost:8080/sockjs');

  sock.onopen = function() {
    console.log('open');
    sock.send('test');
  };

  sock.onmessage = function(e) {
    console.log('message', e.data);
    sock.close();
  };

  sock.onclose = function() {
    console.log('close');
  };
</script>

Supported transports

Requirements

Examples

You can find several examples in the sockjs repository at github.

https://github.com/aio-libs/sockjs/tree/master/examples

License

sockjs is offered under the Apache 2 license.

CHANGES

0.13.0 (2024-06-13)

  • Added argument cors_config into function add_endpoint() to support of CORS settings from aiohttp_cors.

  • Added arguments heartbeat_delay and disconnect_delay into function add_endpoint().

  • Function add_endpoint() now returns all registered routes.

  • Replaced returning instances of error HTTP responses on raising its as exceptions.

  • Changed name of some routes.

  • Heartbeat task moved from SessionManager into Session.

  • Methods _acquire and _release of Sessions renamed into acquire and release.

  • Added processing of ConnectionError in StreamingTransport.

  • Changed arguments of handler function. Now handler function must be defined like async def handler(manager, session, msg):

  • Constants:

    • FRAME_OPEN

    • FRAME_CLOSE

    • FRAME_MESSAGE

    • FRAME_MESSAGE_BLOB

    • FRAME_HEARTBEAT

    replaced by Frame enums with corresponding values.

  • Constants:

    • MSG_OPEN

    • MSG_MESSAGE

    • MSG_CLOSE

    • MSG_CLOSED

    replaced by MsgType enums with corresponding values.

  • Constants:

    • STATE_NEW

    • STATE_OPEN

    • STATE_CLOSING

    • STATE_CLOSED

    replaced by SessionState enums with corresponding values.

0.12.0 (2022-02-08)

  • Breaking change: Removed argument timeout from Session.__init__() and SessionManager.__init__().

  • Breaking change: Argument heartbeat of SessionManager.__init__() renamed into heartbeat_delay.

  • Breaking change: Session.registry renamed into Session.app.

  • Breaking change: Deleted method SessionManager.route_url().

  • Breaking change: Dropped support of Python < 3.7

  • Fixed processing of heartbeats and a session expiration.

  • Fixed ping-pong based heartbeats for web-socket connections.

  • Added arguments heartbeat_delay and disconnect_delay into Session.__init__().

  • Added argument disconnect_delay into SessionManager.__init__().

0.11.0 (2020-10-22)

  • Breaking change: Added into the WebSocketTransport the ability to process multi messages from client (#383).

  • Added into WebSocketTransport ignoring of empty frames received from client. (#383).

  • Added tick after dequeue so heartbeat keeps session live (#265).

  • Fix race condition during iteration over sessions (#217).

  • Support Python 3.8.

  • Fixed examples of using of SockJS server (#264).

0.10.0 (2019-10-20)

  • Sync with aiohttp 3.6 (#298)

0.9.1 (2018-12-04)

  • Minor code styling cleanups

0.9.0 (2018-10-11)

  • Support Python 3.7. The minimal available Python version is 3.5.3 (#240)

0.8.0 (2018-06-15)

  • Fix heartbeat (#214)

0.7.1 (2018-03-05)

  • Fix compatibility with aiohttp 3.0+ again.

0.7.0 (2018-02-25)

  • Fixed compatibility with aiohttp 3.0+ (#169)

0.6 (2017-04-13)

  • Fixed support for aiohttp 2.0+.

0.5 (2016-09-26)

  • Mark SockJSRoute.handler and SockJSRoute.websocket as coroutines. #25

  • Remove a check for “ORIGIN” header #12

  • Process FRAME_MESSAGE_BLOB message type #12

0.4 (2016-02-04)

  • Fixed lost event-loop argument in sockjs.transports.websocket.WebSocketTransport

  • Fixed lost event-loop argument in sockjs.transports.rawwebsocket.RawWebSocketTransport

  • Fixed RawRequestMessage. Add raw_header argument (aiohttp 0.21+)

  • Fixed many warnings

  • Fixed sockjs.route add_endpoint without name bug

0.3 (2015-08-07)

  • Fixed calls of SessionManager.aquire() - was removed the unnecessary second argument.

  • Fixed the incorrect argument in one call of cors_headers().

  • Fixed many errors. The code is not perfect, but at least it was working as it should.

0.2 (2015-07-07)

  • Fixed packaging

0.1.0 (2015-06-21)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sockjs-0.13.0.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

sockjs-0.13.0-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file sockjs-0.13.0.tar.gz.

File metadata

  • Download URL: sockjs-0.13.0.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for sockjs-0.13.0.tar.gz
Algorithm Hash digest
SHA256 57e959a23f20a8d55149d1e5db0b3b870726f2b4ad8166d41bdcf411b36cdf76
MD5 0d35a367e2e82644112d6070d867bf7c
BLAKE2b-256 00a4644d5901d51aecb9fff218bbfc2525ef9362d4d03c2fcb8cb322aa0ce77f

See more details on using hashes here.

File details

Details for the file sockjs-0.13.0-py3-none-any.whl.

File metadata

  • Download URL: sockjs-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for sockjs-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b00ac14e2d95b9f7c3769a475c06745d529e030fdca278a64b2cc9f4ae2c4d0d
MD5 9689f636f02697a9865a781fbfecc106
BLAKE2b-256 882abec7bb955323f799a0a041495843aae0eb5aeeeb5b98e13c05d8c633c6e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page