Git Product home page Git Product logo

wire's Introduction

Wire

🔥 A pub-sub framework built on Kafka

Proof-of-concept, so you probably shouldn’t use it in production

Getting Started

First, install Kafka. With Homebrew, use:

brew install kafka
brew services start zookeeper
brew services start kafka

Add this line to your application’s Gemfile:

gem "wire", github: "ankane/wire"

Publish a message with:

Wire.publish(:visit, {name: "McNulty"})

Create a new consumer with:

rails generate wire:consumer Welcome

It’ll be placed in app/consumers. Update it to:

class WelcomeConsumer < Wire::Consumer
  topic :visit

  def perform(message)
    puts "Hi #{message["name"]}"
  end
end

Run consumers with:

bundle exec wire

Model Updates

Publish model updates from one app

class User < ActiveRecord::Base
  publish :name, :email
end

And subscribe in another app (start consumers as normal)

class User < ActiveRecord::Base
  subscribe :name, :email
end

To sync all records, use:

User.find_each { |user| user.sync }

More Features

To make development easier, you can run consumers immediately with:

WelcomeConsumer.perform_now(message)

To consume a single message, use:

WelcomeConsumer.consume_once

Metadata

Use the metadata method to access info like partition, offset, key, and raw value.

class WelcomeConsumer < Wire::Consumer
  def perform(message)
    metadata[:partition]
    metadata[:offset]
    metadata[:key]
    metadata[:value]
  end
end

Serialization

By default, Wire uses JSON for serialization.

To disable serialization, use:

Wire.default_serializer = :noop

For MessagePack, add msgpack to your Gemfile and use:

Wire.default_serializer = :msgpack

Or create a custom serializer

class CustomSerializer
  def serialize(data)
    data + "!!!"
  end

  def deserialize(data)
    data.chomp("!!!")
  end
end

Wire.register_serializer(:custom, CustomSerializer, default: true)

You can also specify a serializer when publishing

Wire.publish(topic, message, serializer: :msgpack)

Or consuming

class MessageConsumer < Wire::Consumer
  serializer :msgpack
end

TODO

  • Multi-threaded consumers
  • Auto-reload consumers
  • Connection pool for publish
  • Hooks for instrumentation
  • Ability to disable consumers
  • Retries

Credits

Thanks to Promiscuous for designing a great interface for model updates.

wire's People

Contributors

ankane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.