Sam Pettersson
Sam Pettersson

Reputation: 3227

Implementing somewhat real time messages into Laravel

I am currently figuring out how i could in the simplest way build a real time messaging system linked with Laravel.

What i am looking to do is to:

Imagine 2 users, A & B.

  1. A sends a message to B and Laravel saves this in the database and notifies the real time server that a new message is available.
  2. The real time server notifies B of this.
  3. B does an ajax call getting the message.

So i would just want something more simpler than a fully fledged real time messaging system that instead of sending the messages directly just notifies the client that there is a new message.

Would this be possible to achieve using some service, node.js or something else?

Upvotes: 1

Views: 1507

Answers (2)

Arun Taylor
Arun Taylor

Reputation: 1572

You could achieve your goal by using Accord Cloud Service as a notification service. Accord uses ActiveML, a JSON like language for creating real-time applications.

The following Python code example sends a notification to a client browser when an 'interesting' event as defined by the client occurs.

#!/usr/local/bin/python

import sys
import socket

if __name__ == '__main__':

    #
    # Connect with Accord Cloud Service using telnet port
    #

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("ac.accord.com", 23))
    data = s.recv(1024)

    #
    # Login with user and password
    #

    s.send("your-email-address-goes-here\n")
    data = s.recv(1024)
    s.send("your-password-goes-here\n")
    data = s.recv(1024)

    #
    # Ensure Msg variable exists.
    #

    s.send("what is type of element Msg;\n")
    data = s.recv(1024)
    if data.startswith('error'):
            s.send("create string Msg;\n")
            data = s.recv(1024)

    while (1):
            print "Enter message to send to a browser: "
            msg = sys.stdin.readline()
            msg = msg[:-1]
            print("set string Msg = \"%s\";\n" % msg) 
            s.send("set string Msg = \"%s\";\n" % (msg))
            data = s.recv(1024)

    s.close()

PHP, Perl, Java, C/C++, etc. code would be very similar to above. You could also send a notification using JavaScript from a browser, if needed. You could also telnet to the cloud service and accomplish the same thing using ActiveML CLI.

In the above example, every time 'set string Msg' is sent to the cloud service, it notifies all client browsers interested in the item that the value of Msg has changed. Each browser then proceeds to update the page. In the example below, client browser receives a notification/update initiated from the Python program above and it puts up an alert displaying the text was entered.

    http://ac.accord.com/demo/notify.html

To play with it, create a free temporary account by clicking on 'login' button and then follow the directions.

How many concurrent clients are you anticipating, 10, 100, 1000+? Disclosure: I work for Accord Software, Inc.

Upvotes: -2

kok
kok

Reputation: 226

There's an article on Medium which covers process of making real time chat app with Laravel and ReactPHP instead of node.js https://medium.com/on-coding/eaa550829538 Maybe you can find something useful for you in it.

Upvotes: 3

Related Questions