SQL Server Event Notifications & Service Broker - minimum req'd for multiple servers? - sql-server

I'm trying to figure out the easiest way to send SQL Server Event Notifications to a separate server using service broker. I've built an endpoint on each server, a queue on each server, working on Dialogs and Contracts and activation... but do I need any of that?
CREATE EVENT NOTIFICATION says it can send the notification XML to a "target service" - so could I just create a contract on the "sending" server that points to a queue on a "receiving server", and use activation there?
Or do I need to have it send to a local queue and then forward on to the receiving server's queue? Thanks!

You can target the remote service, but you have to have the ROUTEs defined for bidirectional communication so that you get the Acknowledgement message back. I once had a script for creating a centralized processing server for all Event Notifications, and the other servers targeted it's service. If I can find it I'll post it on my blog and update this with a link.

Related

How can I implement SQL Server event listeners to trigger a Nodejs function?

I am new to nodejs and I would like to figure out how I could have event listeners from SQL Server trigger a Nodejs function on my server.
For instance, I would like my Nodejs server to receive notifications when there is any change (insert/update) for any given table in my database, which would then trigger an update of the UI on the client-side.
I'm not altogether familiar with SQL Server, but I don't think you'll want to be using your DB server to POSTs to a webhook endpoint anyhow.
You're better off handling this functionality in your application code rather than on your database server.
The general idea is that it's your application that's going to be operating on the database anyhow, so you should have your application send out the the updates to your UI. A high-level step-by-step would look like so:
Client sends request to server
Server receives request and updates database
Upon a successful update of the database, your server sends a response to the client
The client receives the response from the server and adjusts the UI accordingly

SQL Server with Service Broker

When I do something such as insert, update in SQL Server, and I have a Service Broker queue. I just know do SEND to send the message to the queue.
Q: can the message send to queue automatically? Thanks.
No, there is no built-in way of sending messages in response to changes to a table. You would have to wire this up yourself using triggers or forcing all your table changes to go via stored procedures.

Send TCP Message in Update/Insert/Delete Trigger

I am building a support ticket system using Sql Server 2014, ASP.Net MVC 5, angular JS etc.
As part of the design I want a way for my system to know when a ticket has been updated, deleted, or created.
That way if a user has a ticket open and it is changed while they have it open I can design the system to force them to refresh the ticket before they themselves can make changes to it, to prevent User B from overriding User A's changes they haven't seen.
Ideally, I'd like to design a TCP Protocol server as a Windows Service and be able to connect to it and send it data from table triggers in Sql Server.
Then the application front end would use Javascript and WebSockets. So the application would be connected to the socket server as well as sql server. When a user opens a ticket I would send a message that user XXY has Ticket 00X open. When a change happens in sql server it tells the server Ticket 00X changed. Then the Socket server tells clients connected to it that are looking at Ticket 00X that it has changed and the javascript prevents a submit until a fresh is done.
But... Can sql server do this at all? Doesn't appear so.
So I'm wondering if it's posisble to build a plugin for SQL Server to enable support for it like PostgreSQL's Notify feature.
Update:
I've discovered User Defined CLR Functions in SQL Server and have managed to get it working. (C#/.Net Framework) I made a static class with some static methods like,
public static int NotifyTicketUpdate(int ticketID)
{
//...
}
Then I registered it in SQL Server,
USE TLCDB;
CREATE ASSEMBLY MyCompanyName_MyDll
FROM 'd:\pathtodll\mydll.dll'
WITH PERMISSION_SET = SAFE;
CREATE FUNCTION XYZ_Notify_Ticket_Updated(#input int) RETURNS int
AS EXTERNAL NAME MyCompanyName_MyDll.UserDefinedFunctions.NotifyTicketUpdated;
Then to call it in SQL, I just do
select dbo.XYZ_Notify_Ticket_Updated(#ticketIDHere);
And it all works. My Static method in c# sends the TCP/IP message to my socket server, the server then checks to see who is looking at that ticket ID and sends them a Ticket_Updated message. The websocket layer running in client javascript sees it, and locks the ticket for updates/saves.
Or you can use Service Broker for handling asynchronous notifications. Not the simplest thing to learn, but lightweight, scalable and already built-in.
You could use CLR, which requires a bit of setup.
You could create an EXE that you can shell with parameters from an SP.
You could implement some standard concurrency. Optimistic vs Pessimistic
So yes, it's possible.

Service Broker configuration

I have configured Service Broker communication between two SQL Server 2008 instances using Windows authentication. I am sending a message from Initiator Service to Target Service and then ending the conversation in the target. Since target is not sending a reply message back to initiator, does the Target Instance need to have any Route configured for sending system generated acknowledgement messages to Initiator? Can I only rely on conversation handle in Target instance to communicate back to Initiator?
Thanks in advance.
A route is always needed in both directions. Even if you never send messages explicitly from the target, the target still needs to send implicit acknowledgements for each message received.
But in your case you are sending an explicit message: END CONVERSATION sends an http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog message.

SQL Server Message Broker - External Activation

I have a Sql Server inside a restricted network. I need to somehow get data from the outside in.
I would like to harness the use of Message Broker. My thinking is the external db places a message on a queue then I require a service that sits inside of the restricted LAN to listen (poll?) for these messages and then act upon them.
I cannot have the external queue initiate the normal broker conversation into the restricted LAN.
My question is should I be looking at the broker external activator to sit inside the restricted LAN and listen for new messages and then act upon them? Has anyone got any experience with this. Documentation / examples for external activator are pretty thin on the ground and monologues are not supported in message broker yet.
Is msmq a better option?
My recommendation would be to allow Service Broker to deliver the message all the way into the SQL Server instance inside the restricted lan. That will require the restricted LAN to allow incomming connection (allow the inside server to listen and accept). MSMQ would be no different, the MSMQ port(s) would have to be open in the restricted LAN.
If you want to use a dedicated process inside the restricted LAN that 'gets' the data inside then you must ensure the transactional consistency between the external server 'get' and the internal server write: the two operation have to be enrolled into a distributed transaction and the DTC protocol itself needs to be allowed to penetrate into the restricted LAN. So some ports still need to be open in the restricted LAN.
What your LAN security designers need to understand is that Service Broker connections are not Transact-SQL connections. Service Broker uses a dedicated protocol that only allows exchange of Service Broker messages. All traffic is encrypted and secured with RC4 or AES encryption. SSB cryptography is FIPS compliant. Allowing for Service Broker traffic to the SQL Server inside is probably the most secure way of allowing data from the external server to reach the secured server. In Service Broker networking there is no concept of 'client' and 'server' and one cannot design the network allowing connections only in one dirrection (eg. unlike say HTTP, which can be designed to connect from inside to outside but not the other way). SSB networking requires both machines involved to be able to connect to each other, because response messages can come after long delays (hours, days, consider the case when a queue is backed up so it takes a long time until the message is processed and a response is sent). IS not feasable to keep connecitons open for days to expect a response, so the receiver of a message must be able to connect back to the sender to deliver a response.

Resources