1

I'm trying to develop chat application software using visual C# and Firebird as the database.

When a client a sends a message, I want client B to be notified.

How could I notify client B from the database that a message is waiting?

Assuming that I created an event table in my database that when an insert is made to the message table, how would I catch the message generated by the database in C#?

Can somebody guide me what code I should use in C# just to catch the notification of the database and display it to client B?

7
  • 2
    this task is more suited to a messaging queue than a database; you can save the logs from the chat into the database for archiving if you wish, but monitoring the database for inserts isn't practical. Try SignalR, or RabbitMQ.
    – Claies
    Mar 12, 2015 at 5:26
  • oh ok. I'll try your suggestion. hopefully I can understand
    – Mandz
    Mar 12, 2015 at 5:36
  • @Claies Firebird has events that can be posted (for example in a after insert trigger), which clients can subscribe to (which then needs to check the database for the actual data). Although I agree that for a chat application that probably isn't ideal. Mar 12, 2015 at 20:39
  • @MarkRotteveel well I'm trying to implement it that way although my dilemma is how would c# catch the event thrown by the firebird. I'm trying to study other concepts on how would I implement the Chat application. Do you have an idea how to catch the event of firebird on c# or on other language?
    – Mandz
    Mar 13, 2015 at 0:54
  • 1
    The Firebird .net provider has an event manager which you can use to subscribe and get notified. I don't have the exact details available right now. In Java it is the FBEventManager class. Mar 13, 2015 at 6:22

1 Answer 1

1

Below is a very simple program that handles events, it was copied/modified from Firebird .NET - Examples of use.

The class that handles the events is FbRemoteEvent. You register the event(s) you are interested in on an instance and you add event handler(s) to be notified when an event occurs. Events are notified asynchronously (although the example doesn't clearly demonstrate that). The event count is the number of events since the last (processed) event notification.

To simplify the example I have used an execute block to directly post the event (instead of - for example - posting the event from a trigger). Note that in this example the connection notifies itself of the event, but it would have worked exactly the same if the notification was coming from a different connection:

class Program
{
    static void Main(string[] args)
    {
        FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
        cs.DataSource = "localhost";
        cs.Database = @"TESTDATABASE.FDB";
        cs.UserID = "SYSDBA";
        cs.Password = "masterkey";
        cs.Charset = "NONE";
        cs.Pooling = false;

        FbConnection connection = new FbConnection(cs.ToString());
        connection.Open();

        FbRemoteEvent revent = new FbRemoteEvent(connection);
        revent.AddEvents(new string[] { "new_order" });

        // Add callback to the Firebird events
        revent.RemoteEventCounts += new FbRemoteEventEventHandler(EventCounts);

        // Queue events
        revent.QueueEvents();

        string sql = "EXECUTE BLOCK AS BEGIN POST_EVENT 'new_order'; END";

        FbCommand command = new FbCommand(sql, connection);

        for (int i = 0; i < 5; i++)
        {
            command.ExecuteNonQuery();
        }

        System.Threading.Thread.Sleep(2000);
        connection.Close();
        Console.ReadLine();
    }

    static void EventCounts(object sender, FbRemoteEventEventArgs args)
    {
        Console.WriteLine("Event {0} has {1} counts.", args.Name, args.Counts);
    }
}
5
  • this might become useful. I'll try it right away. Do you have a link for firebird tutorial like HELLO world. I mean useful examples in coding events and triggers. I'm kinda beginner in Firebird programming. thx...
    – Mandz
    Mar 18, 2015 at 1:38
  • You can look at the Interbase 6 documentation that you can download from the Firebird website. It also contains simple examples (iirc in the language references). Mar 18, 2015 at 8:11
  • Can you show me the direct link especially on the Interbase 6 documentation since google always redirects me on firebirdsql.org and couldn't find what I am looking for. thx
    – Mandz
    Mar 19, 2015 at 1:17
  • 1
    That is because it is hosted on firebirdsql.org, see firebirdsql.org/en/reference-manuals near the end of the page. Mar 19, 2015 at 16:35
  • atleast I know now where to start reading. Will start reading right away. It's really helpful thanks
    – Mandz
    Mar 20, 2015 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.