Polytalk Ruby Gem updated

I just pushed some changes to the Polytalk Ruby Gem to allow symbols to be used as arguments. Als...

Sugar, the ActiveSupport of the JavaScript world

Sugar is an amazing JavaScript library that extends a lot of the native JavaScript types and brin...

0
Sunday, December 9, 2012
Using Polytalk inside a Meteor Application

I've put together a simple demo that integrates Polytalk with Meteor. The app allows you to retrieve, create and delete posts from a Ruby Polytalk server that is exposing a simple Post ActiveRecord model.

The app utilizes Meteor's latency compensation and keeps the database updated on both the Meteor app and the Ruby server. We do this by loading in all the posts from the server into a Meteor collection that we keep updated every time you create or delete a post.

For the most part the code will look familiar to anyone who's played around with a Meteor app before. We have 3 methods on the server that we expose to the client that will load posts, save a post and remove a post. The one tricky part is in the loadPosts method that synchronizes the Meteor collection with the posts from the Ruby server.

# Load posts from Polytalk
loadPosts = ->

  # Remove all posts from collection
  Posts.remove({})

  postsRequest =
    class: 'Post'
    method: 'find'
    arguments:
      type: ':all'
      options:
        order: 'id DESC'
        limit: 10

  client.call postsRequest, (response) ->
    posts = response.map (post) -> post.post
    for post in posts
      Fiber ->
        Posts.insert({dbid: post.id, title: post.title, body: post.body, time: Date.now()})
      .run()

  return true

Once we receive the posts from the server we need to insert the posts into the collection in a Fiber so it doesn't lock up other parts of the application.

Run the demo

The git repository has the app and the ruby server included. To get it running you will need to cd into the ruby_server directory and run bundle to download the gems. Once downloaded while in the ruby_server directory run ruby server.rb to start the Polytalk server. This will setup the database and expose the Post model to the client.

Once the Ruby server is running you can cd back to the root of the project and run the Meteor app with meteor. You should now be able to visit the app in your browser and start adding and removing posts.

That's it, let me know if you have any questions or suggestions below in the comments.

Tweet this post if you found it useful

Related Posts

Introducing Polytalk. A simple protocol which allows communication between different languages via TCP.

Monday, November 26, 2012  ·   0

Polytalk is my latest open source project. I developed it out of the need to communic... (continued)

Polytalk Ruby Gem updated

Saturday, December 8, 2012  ·   0

I just pushed some changes to the Polytalk Ruby Gem to allow symbols to be used as ar... (continued)

Building a simple single page application using AngularJS

Tuesday, December 4, 2012  ·   0

I am going to walk you through building a SPA (single page application) using Angular... (continued)

Tips for dynamic CSS themes using jQuery

Friday, November 30, 2012  ·   0

I recently added a dark theme to this blog, it's great for night time reading on ... (continued)

0 Comments

Comment successfully added. View comment.×

Comments are now closed for this article.