Download the PHP package leemason/polycast without Composer
On this page you can find all versions of the php package leemason/polycast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download leemason/polycast
More information about leemason/polycast
Files in leemason/polycast
Package polycast
Short Description Laravel Websocket broadcasting polyfill using ajax and mysql.
License MIT
Informations about the package polycast
Polycast
Laravel Websocket broadcasting polyfill using ajax and mysql. Laravel 5.1 or Later
Installation
Require this package with composer:
After updating composer, add the ServiceProvider to the providers array in config/app.php
Laravel 5.1:
Add the following in your broadcasting connections array located in config/broadcasting.php
Copy the package assets to your public folder with the publish command:
Migrate the packages database migrations (creates the polycast_events table):
Usage
To Optionally set Polycast as your default broadcast events driver set as the default in your config/broadcasting.php or in your .env file.
Once installed you create broadcastable events exactly the same as you do now (using the ShouldBroadcast trait), except you have a way to consume those events via browsers without the need for nodejs/redis or an external library to be installed/purchased.
This package doesn't aim to compete with libraries or solutions such as PRedis/SocketIO/Pusher. But what it does do is provide a working solution for situations where you can't install nodejs and run a websocket server, or where the cost of services like Pusher aren't feasible.
The package utilizes vanilla javascript timeouts and ajax requests to "simulate" a realtime experience. It does so by saving the broadcastable events in the database, via a setTimeout javascript ajax request, polls the packages receiver url and distrubutes the payloads via javascript callbacks.
To add to the simulation of realtime events each event found is parsed from the time its requested, and when the event was fired. The difference in seconds is then used to delay the callbacks firing on that specific event.
What this does is prevent every event callback dumping into the dom when the ajax request has completed, but instead fires then in sequence as if it was loading live.
To the user the only real difference to websockets is that they will be a few seconds behind (depending on the polling option provided "default 5 seconds").
I have tried to keep the javascript api similar to current socket solutions to reduce the learning curve.
Here's an example:
Breaking down the example you can see we include the library:
Create a Polycast object inside a self executing function (this can be done a few ways, and has a few options):
We register any callbacks on the connection events:
We create channel objects by subscribing to the channel:
And we register callbacks for specific events fired on those channels:
Should something go wrong, or you need to disconnect you can at any point in time:
And that's it! (for now)
Bower Usage
The polycast package is registered on Bower using the name and can be installed by running:
The package script can then be accessed from the path.
NPM Usage
The polycast package is registered on npm using the name and can be installed by running:
The package script can then be accessed from the path.
Webpack Usage
The polycast package script files are generated using gulp/webpack, this offers advantages when developing your javascript via script loaders.
Usage is as follows:
The package is still in early development (but is stable) so expect new methods and features soon.
FAQ
Does this require jQuery?
Nope, all vanilla js here including the ajax requests.
What if there is a problem during the request? Will my javascript enter a loop?
Nope, the next setTimeout call wont happen until the previous one has been compeleted.
How does it work out what events get sent to who?
This is done by the channel and event names, but the package also monitors times. When the js service creates a connection the server sends back its current time. This is stored in the js object and is sent/updated on subsequent requests creating a "events named ? on channel ? since ?" type database query.
Notes
The is my first real javascript heavy package, which is great as it gives me more opportunity to learn the language. That being said if there are any improvements you could make please let me know or send a pull request.
The Future
- Add authorization options to channels
- Add helpers here and there for removing channel/event subscriptions
- Add wildcard event name listening
- Add ability to subscribe to events without supplying channel.