在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tlaverdure/laravel-echo-server开源软件地址(OpenSource Url):https://github.com/tlaverdure/laravel-echo-server开源编程语言(OpenSource Language):TypeScript 99.6%开源软件介绍(OpenSource Introduction):Laravel Echo ServerNodeJs server for Laravel Echo broadcasting with Socket.io. System RequirementsThe following are required to function properly.
Additional information on broadcasting with Laravel can be found on the official docs: https://laravel.com/docs/master/broadcasting Getting StartedInstall npm package globally with the following command: $ npm install -g laravel-echo-server Initialize with CLI ToolRun the init command in your project directory: $ laravel-echo-server init The cli tool will help you setup a laravel-echo-server.json file in the root directory of your project. This file will be loaded by the server during start up. You may edit this file later on to manage the configuration of your server. API ClientsThe Laravel Echo Server exposes a light http API to perform broadcasting functionality. For security purposes, access to these endpoints from http referrers must be authenticated with an APP id and key. This can be generated using the cli command: $ laravel-echo-server client:add APP_ID If you run In this example, requests will be allowed as long as the app id and key are both provided with http requests. Request Headers
Authorization: Bearer skti68i...
or
http://app.dev:6001/apps/APP_ID/channels?auth_key=skti68i... You can remove clients with Run The Serverin your project root directory, run $ laravel-echo-server start Stop The Serverin your project root directory, run $ laravel-echo-server stop Configurable OptionsEdit the default configuration of the server by adding options to your laravel-echo-server.json file.
DotEnvIf a .env file is found in the same directory as the laravel-echo-server.json file, the following options can be overridden:
Running with SSL
Note: This library currently only supports serving from either http or https, not both. Alternative SSL implementationIf you are struggling to get SSL implemented with this package, you could look at using a proxy module within Apache or NginX. Essentially, instead of connecting your websocket traffic to https://yourserver.dev:6001/socket.io?..... and trying to secure it, you can connect your websocket traffic to https://yourserver.dev/socket.io. Behind the scenes, the proxy module of Apache or NginX will be configured to intercept requests for /socket.io, and internally redirect those to your echo server over non-ssl on port 6001. This keeps all of the traffic encrypted between browser and web server, as your web server will still do the SSL encryption/decryption. The only thing that is left unsecured is the traffic between your webserver and your Echo server, which might be acceptable in many cases. Sample NginX proxy config
Sample Apache proxy config
Setting the working directoryThe working directory in which SubscribersThe Laravel Echo Server subscribes to incoming events with two methods: Redis & Http. RedisYour core application can use Redis to publish events to channels. The Laravel Echo Server will subscribe to those channels and broadcast those messages via socket.io. HttpUsing Http, you can also publish events to the Laravel Echo Server in the same fashion you would with Redis by submitting a Request Endpoint POST http://app.dev:6001/apps/your-app-id/events?auth_key=skti68i...
Request Body {
"channel": "channel-name",
"name": "event-name",
"data": {
"key": "value"
},
"socket_id": "h3nAdb134tbvqwrg"
}
channel - The name of the channel to broadcast an event to. For private or presence channels prepend PusherThe HTTP subscriber is compatible with the Laravel Pusher subscriber. Just configure the host and port for your Socket.IO server and set the app id and key in config/broadcasting.php. Secret is not required. 'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_KEY'),
'secret' => null,
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'host' => 'localhost',
'port' => 6001,
'scheme' => 'http'
],
], You can now send events using HTTP, without using Redis. This also allows you to use the Pusher API to list channels/users as described in the Pusher PHP library HTTP APIThe HTTP API exposes endpoints that allow you to gather information about your running server and channels. Status Get total number of clients, uptime of the server, and memory usage. GET /apps/:APP_ID/status Channels List of all channels. GET /apps/:APP_ID/channels Channel Get information about a particular channel. GET /apps/:APP_ID/channels/:CHANNEL_NAME Channel Users List of users on a channel. GET /apps/:APP_ID/channels/:CHANNEL_NAME/users Cross Domain Access To APICross domain access can be specified in the laravel-echo-server.json file by changing Example below: {
"apiOriginAllow":{
"allowCors" : true,
"allowOrigin" : "http://127.0.0.1",
"allowMethods" : "GET, POST",
"allowHeaders" : "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
This allows you to send requests to the API via AJAX from an app that may be running on the same domain but a different port or an entirely different domain. DatabaseTo persist presence channel data, there is support for use of Redis or SQLite as a key/value store. The key being the channel name, and the value being the list of presence channel members. Each database driver may be configured in the laravel-echo-server.json file under the RedisFor example, if you wanted to pass a custom configuration to Redis: {
"databaseConfig" : {
"redis" : {
"port": "3001",
"host": "redis.app.dev",
"keyPrefix": "my-redis-prefix"
}
}
}
Note: No scheme (http/https etc) should be used for the host address A full list of Redis options can be found here. Redis sentinelFor example, if you wanted to use redis-sentinel, you need to pass a custom configuration : "databaseConfig": {
"redis": {
"sentinels": [
{
"host": "redis-sentinel-0",
"port": 26379
},
{
"host": "redis-sentinel-1",
"port": 26379
}
{
"host": "redis-sentinel-2",
"port": 26379
}
],
"name": "mymaster",
"sentinelPassword": "redis-password"
},
}, For more information about redis sentinel configuration you can check this SQLiteWith SQLite you may be interested in changing the path where the database is stored. {
"databaseConfig" : {
"sqlite" : {
"databasePath": "/path/to/laravel-echo-server.sqlite"
}
}
} Note 1: The path is relative to the root of your application, not your system. Note 2: node-sqlite3 is required for this database. Please install before using.
Presence ChannelsWhen users join a presence channel, their presence channel authentication data is stored using Redis. While presence channels contain a list of users, there will be instances where a user joins a presence channel multiple times. For example, this would occur when opening multiple browser tabs. In this situation "joining" and "leaving" events are only emitted to the first and last instance of the user. Optionally, you can configure laravel-echo-server to publish an event on each update to a presence channel, by setting {
"database": "redis",
"databaseConfig": {
"redis" : {
"port": "6379",
"host": "localhost"
},
"publishPresence": true
}
} You can use Laravel's Redis integration, to trigger Application code from there: Redis::subscribe(['PresenceChannelUpdated'], function ($message) {
var_dump($message);
}); Client Side ConfigurationSee the official Laravel documentation for more information. https://laravel.com/docs/master/broadcasting#introduction TipsSocket.io client libraryYou can include the socket.io client library from your running server. For example, if your server is running at
Note: When using the socket.io client library from your running server, remember to check that the µWebSockets deprecationµWebSockets has been officially deprecated. Currently there is no support for µWebSockets in Socket.IO, but it may have the new ClusterWS support incoming. Meanwhile Laravel Echo Server will use |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论