The UK Department of Trade and Investment regularly runs trade missions to the Bay Area; yesterday the UK TI ‘Cloud Mission’, a group of British entrepreneurs working with cloud technology, visited us here at salesforce.com, and I had the pleasure of presenting our Social Enterprise Platform (database.com, Force.com and Heroku) to them, and chatting with some of them about their companies and technology.

Among the delegates was Max Williams, CEO of Pusher, a startup providing real-time updates (in the sense of WebSockets) as-a-service, and Max encouraged me to try Pusher out. How could I resist? Since there’s already a Heroku Pusher add-on, I decided to try pushing events from Apex code running on Force.com.

Looking at the Pusher docs, it’s all very straightforward – you drop some JavaScript into a web page that allows you to subscribe for updates on a channel. There’s a REST API for pushing events to the channel, and it took all of half an hour to get it working from Apex. It’s very similar to our Streaming API, except that an app explicitly publishes updates to a channel, rather than the platform generating events as data is modified.

Since the Apex implementation comprises all of 51 lines of code, rather than spinning up a GitHub project, I decided to just post it here, since it’s a nice little example of calling a REST API from Apex.

So, here’s the browser code, in a Visualforce Page:

A real app would do something more interesting with the data than just pop up an alert, but you get the idea, I’m sure 🙂

The Apex Pusher class:

Apex code to invoke the Pusher class:

And proof that it works 🙂

Although I just pushed a string here, you could just as easily push JSON data:

So, go give Pusher a spin, and let us know in the comments if you’ve implemented an interesting API from Apex recently.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS