Last active
April 25, 2019 13:41
-
-
Save rcknight/5091329 to your computer and use it in GitHub Desktop.
Example Event Store SignalR publisher. For real-time push of events to a web page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using EventStore.ClientAPI; | |
using Microsoft.AspNet.SignalR; | |
using Microsoft.Owin.Hosting; | |
using Owin; | |
namespace EventStoreSignalR | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var esClient = EventStore.ClientAPI.EventStoreConnection.Create(); | |
esClient.Connect(new IPEndPoint(IPAddress.Loopback,1113)); | |
using (WebApplication.Start<Startup>("http://localhost:8081")) | |
{ | |
Console.WriteLine("SignalR Server running."); | |
esClient.SubscribeToAll(true,SendToClient); | |
Console.ReadLine(); | |
} | |
} | |
public static void SendToClient(ResolvedEvent e) | |
{ | |
var groupName = e.OriginalStreamId; | |
var context = GlobalHost.ConnectionManager.GetHubContext<EventStoreHub>(); | |
var theGroup = context.Clients.Group(groupName); | |
var data = Encoding.UTF8.GetString(e.OriginalEvent.Data); | |
var metadata = Encoding.UTF8.GetString(e.OriginalEvent.Metadata); | |
theGroup.handleEvent(new | |
{ | |
eventId = e.OriginalEvent.EventId, | |
eventNumber = e.OriginalEvent.EventNumber, | |
eventStreamId = e.OriginalEvent.EventStreamId, | |
eventType = e.OriginalEvent.EventType, | |
data, | |
metadata | |
}); | |
} | |
} | |
class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
var config = new HubConfiguration { EnableCrossDomain = true }; | |
app.MapHubs(config); | |
} | |
} | |
public class EventStoreHub : Hub | |
{ | |
public Task Subscribe(string streamName) | |
{ | |
return Groups.Add(Context.ConnectionId, streamName); | |
} | |
public Task Unsubscribe(string streamName) | |
{ | |
return Groups.Remove(Context.ConnectionId, streamName); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>SignalR Test</title> | |
<script type="text/javascript" src="./jquery.min.js"></script> | |
<script type="text/javascript" src="./jquery.signalR.min.js"></script> | |
<script type="text/javascript" src="http://localhost:8081/signalr/hubs"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
$.connection.hub.url = "http://localhost:8081/signalr"; | |
var eventStoreHub = $.connection.eventStoreHub; | |
$.connection.hub.start() | |
.done(function() { | |
$("body").append("Connected!"); | |
}) | |
.fail(function(ex) {alert("failed to connect!" + JSON.stringify(ex));}); | |
eventStoreHub.client.handleEvent = function (event) { | |
$("body").append("<pre>" + JSON.stringify(event) + "</pre>"); | |
} | |
function subscribe() { | |
var streamName = $("#streamName").val(); | |
eventStoreHub.server.subscribe(streamName) | |
.done(function() { | |
alert("subscribed."); | |
}) | |
.fail(function() { | |
alert("subscribing failed"); | |
}); | |
} | |
function unsubscribe() { | |
var streamName = $("#streamName").val(); | |
eventStoreHub.server.unsubscribe(streamName) | |
.done(function() { | |
alert("unsubscribed."); | |
}) | |
.fail(function() { | |
alert("unsubscribing failed"); | |
}); | |
} | |
</script> | |
<input id="streamName" type="text" value="stream name"></input> | |
<button id="subscribeButton" onClick="subscribe()">Subscribe</button> | |
<button id="unsubscribeButton" onClick="unsubscribe()">Unsubscribe</button> | |
</body> | |
</html> |
can you please explain how to integrate this code in an MVC application
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Packages from nuget:
Install-Package EventStore.Client
Install-Package Microsoft.Owin.Hosting -pre
Install-Package Microsoft.Owin.Host.HttpListener -pre
Install-Package Microsoft.AspNet.SignalR.Owin