Languages Magazine

The MindMeld API: Introducing Real-Time Push Events

By Expectlabs @ExpectLabs

The MindMeld API uses push events to notify applications when entity lists have been generated. In the video above, learn how you can start using this powerful feature of the MindMeld platform. 

Follow up with this video for an overview of the Crawl Manager.

TRANSCRIPT:

In this screencast, I’ll show you how to use real-time push events. Real-time push events are a way of pushing out events to a number of clients so that each of those clients that subscribe to that channel will see the same event at the same time. It’s just an easy way to broadcast messages. So to start, go to http://developer.expectlabs.com. I’ll go into the documentation, down into the JavaScript SDK section, and I’m going to download the JavaScript SDK for MindMeld. Now I’ve already done it on my machine here so I’ll click on the link, but if I haven’t done this, I’ll go to the location where it was downloaded. Now I’ve put in that location a skeletal file, which means an example. This is a very simple file, it has some utility functions, some CSS, and a very minimal HTML, which I’m going to log events to. So we go look at this now, we can see it doesn’t really do anything very interesting. Now what I’m going to do is, I’m going to use the MindMeld API to subscribe to customer events on a user channel. I’m going to show how later on we can go to the API Explorer, or to the command window and publish events and you’ll see them coming up in my browser window.

Let’s just go start writing out our program. The first thing we do is, we want to connect to the MindMeld API. First of all, I need to get my app ID. I’ll go to my management console and I’ll copy that out. Now I have my app ID, so when jQuery loads the page, it will trigger my little code here or set up this config object and then I’ll actually initialize the API. So now this mm object is my interface to the MindMeld API. Let’s do something a little more interesting, let’s get ourselves the user token. This onSuccess is the callback that gets called when the GET user token call succeeds. And so for the purposes of this video, we’re not going to have our error handlers just to make the code easier to understand. But in practice we’d always want to have a callback function in the case where the call doesn’t succeed. I’m sending some credentials to get a user token and some app secret again is going to come from the management console. When it succeeds I want to create a session and I’ll just call it a dummy. I’ll stop it here for now. have a new user, it has ID 312. And now, we have a user token if we want to go through and create a session, and so it’s pretty much the same thing. We’ll call the MindMeld active user sessions POST with the name of the session, and we’ll have a callback for when that succeeds. And so again in the callback function which is the next step in my chain which is to actually subscribe to a channel. So I’ll just put a STOP function in for now. Okay, so now I’m doing a POST into the sessions endpoint to create my new session. I give it a name, I make it an invite-only session. That looks okay, there’s no output expected.

Next we’d like to subscribe to a channel. And so again, I’m subscribing through a user channel with a custom user event name. And it’ll call the handle update, which is a utility function I have elsewhere. That’s it, see that should work. Now if I go into the API Explorer I can actually publish a new event. So I need to have the same user ID as the user here, so it’s user 312. I go to POST-user-events. And I say, “custom.” This is going to post an event with a name, custom user name, and a payload, “Hello, World.” We can see down here that I produce a equivalent command line, by doing that curl command. So if I do that, and if I go look at my thing, I can see that it says, “event received, Hello World.” And so the MindMeld SDK running inside the browser had subscribed to that channel. So we’ve shown here how I can write code using one of our SDKs to connect to the MindMeld API and receive custom events, and I showed how you could publish that event using the API Explorer, but you could also have done by the curl command or programmatically in whatever language you care to use.


Back to Featured Articles on Logo Paperblog

Paperblog Hot Topics

Magazines