GithubHelp home page GithubHelp logo

ring-clojure / ring-websocket-async Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 2.0 28 KB

Library for using core.async with Ring's websocket API

License: MIT License

Clojure 100.00%
clojure ring-clojure websockets

ring-websocket-async's Introduction

Ring-Websocket-Async Build Status

A Clojure library for using core.async with Ring's websocket API (currently in beta testing).

Installation

Add the following dependency to your deps.edn file:

org.ring-clojure/ring-websocket-async {:mvn/version "0.2.0"}

Or to your Leiningen project file:

[org.ring-clojure/ring-websocket-async "0.2.0"]

Breaking Changes

In 0.1.0 the go-websocket block would automatically close the WebSocket after it completes. In 0.2.0 this was removed, as it prevented easy use of functions like clojure.core.async/pipe, which have their own internal go blocks.

Usage

The most convenient way to get started is to use the go-websocket macro. Here's an example that echos any message received back to the client:

(require '[clojure.core.async :as a :refer [<! >!]]
         '[ring.websocket.async :as wsa])

(defn echo-websocket-handler [request]
  (wsa/go-websocket [in out err]
    (loop []
      (when-let [mesg (<! in)]
        (>! out mesg)
        (recur)))))

The macro sets three binding variables:

  • in - the input channel
  • out - the output channel
  • err - the error channel

Then executes its body in a core.async go block. If the WebSocket is closed by either the client or the server, the associated channels will also be closed.

A Ring websocket response will be returned by the macro, so you can directly return it from the handler.

The error channel may be omitted. In this case, any errors with the websocket protocol will close the connection with a 1011 unexpected server error message.

To close the connection from the server with a specific error code of your choice, you can use the closed function to send a special message to the output channel:

(defn closes-with-error [request]
  (wsa/go-websocket [in out err]
    (>! out (wsa/closed 1001 "Gone Away"))))

If you want more control, you can use the lower-level websocket-listener function. The following handler example is equivalent to the one using the go-websocket macro:

(defn echo-websocket-handler [request]
  (let [in  (a/chan)
        out (a/chan)
        err (a/chan)]
    (go (try
          (loop []
            (when-let [mesg (<! in)]
              (>! out mesg)
              (recur)))
          (finally
            (a/close! out))))  ;; closes the websocket
    {:ring.websocket/listener (websocket-listener in out err)}))

License

Copyright © 2024 James Reeves

Released under the MIT license.

ring-websocket-async's People

Contributors

julianwegkamp avatar weavejester avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ring-websocket-async's Issues

Version for ring 2.0.0-alpha1

This library has been working just great for me. Thanks for creating it. What would be involved with getting it to work with ring 2.0.0-alpha1? (just wondering).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.