Getting Started - PeteGoo/Pushqa GitHub Wiki
##Setting up the Pushqa server Create a new ASP.Net Empty Web Application project in Visual Studio.
If you are installing via NuGet, right-click your project and choose "Manage NuGet packages". Search for Pushqa and choose to install Pushqa.Server.
First we are going to need to create a server side context that will produce our Observable event stream. Add a class called MyPushContext. Add the following code which will push an event every 1 second with an id, the timestamp and the detail "Message".
public class MyPushContext {
public IQbservable<MyMessage> OneSecondTimer {
get {
return Observable.Interval(TimeSpan.FromSeconds(1))
.Timestamp()
.Select(i => new MyMessage {
MessageId = i.Value,
TimeStamp = i.Timestamp,
Description = "Message"
})
.AsQbservable();
}
}
It uses the following message class definition, this is the type of message we will send to our clients.
public class MyMessage {
public long? MessageId { get; set; }
public DateTimeOffset TimeStamp { get; set; }
public string Description { get; set; }
}
If your ASP.Net project does not already have one, add a Global.asax page to your web application. In the Application_Start method add the following code.
protected void Application_Start(object sender, EventArgs e) {
RouteTable.Routes.MapConnection<QueryablePushService<MyPushContext>>("events", "events/{*operation}");
}
This will map any incoming requests to the "events" path in our web application onto our MyPushContext class.
##Setting up a javascript client Add an ASPX or HTML page to the project. Use the following code to collect each incoming event. Note that this query will only listen for even numbered events, skip the first 2 and take 5 in total before stopping.
<div>
<script type="text/javascript">
$(function () {
var connection = $.connection('../events/OneSecondTimer/', { $filter: "(MessageId mod 2) eq 0", $skip: 2, $top: 5 });
connection.received(function (data) {
if (data.Type == 'Completed') {
connection.stop();
$('#messages').append('<li>Complete</li>');
}
else {
$('#messages').append('<li>' + data.Message + '</li>');
}
});
connection.starting(function () {
$('#messages').append("'<li>Starting Connection</li>");
});
connection.start();
$("#connect").click(function () {
connection.start();
});
$("#disconnect").click(function () {
connection.stop();
});
});
</script>
<input type="button" id="connect" value="Connect" />
<input type="button" id="disconnect" value="Disconnect" />
<ul id="messages"></ul>
</div>
Or you can now use Reactive Extensions for Javascript
<script type="text/javascript">
$(function () {
// Setup our connection
var connection = $.connection('../events/OneSecondTimer/', { $filter: "(MessageId mod 2) eq 0", $skip: 2, $top: 5 });
// Append each message received
connection.asObservable().subscribe(
function (data) {
// onNext
$('#messages').append('<li>' + data + '</li>');
},
function (error) {
// onError
$('#messages').append('<li>Error: ' + error + '</li>');
},
function () {
// onCompleted
$('#messages').append('<li>Complete</li>');
}
);
connection.start();
$("#connect").click(function () {
connection.start();
});
$("#disconnect").click(function () {
connection.stop();
});
});
</script>
<input type="button" id="connect" value="Connect" />
<input type="button" id="disconnect" value="Disconnect" />
<ul id="messages"></ul>
##Setting up a .Net Client Add a Console Application to the solution. From NuGet package manager choose to install the "Pushqa" package. Create a new class called MyPushEventProvider. This will be our client proxy class which will talk to our server. Add the following code to the class substituting the address of our server project.
public class MyPushEventProvider : EventProvider {
public MyPushEventProvider() : base(new Uri("http://localhost:14844/events")) { }
public EventQuerySource<MyMessage> OneSecondTimer {
get { return CreateQuery<MyMessage>("OneSecondTimer"); }
}
}
Now, make sure that the console application has access to our message type and in the Main method of our Program class we can add the following code to write out the incoming events.
// Setup the event stream subscription
MyPushEventProvider eventProvider = new MyPushEventProvider();
(from myEvent in eventProvider.OneSecondTimer
where myEvent.MessageId % 2 == 0
select myEvent)
.Take(5)
.AsObservable()
.Subscribe(message => Console.WriteLine("Id={0}, TimeStamp={1}, Description={2}", message.MessageId, message.TimeStamp, message.Description));