60 lines
1.3 KiB
Markdown
Raw Normal View History

2016-04-04 21:51:00 +02:00
# WebSockets
2016-03-22 18:11:11 +01:00
[Socket.IO](http://socket.io/) enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.
2016-04-21 11:39:18 +02:00
!!! warning
Strapi doesn't handle WebSockets using multiple nodes yet. WebSockets only work on the master process. You can disable the WebSockets and implement Socket.IO with Redis on your own. Feel free to [join us on Slack](http://slack.strapi.io/) to talk about it.
2016-03-22 18:11:11 +01:00
## Configuration
Configuration:
- Key: `websockets`
- Environment: all
- Location: `./config/general.json`
- Type: `boolean`
Example:
```js
{
"websockets": true
}
```
Notes:
2016-04-04 21:51:00 +02:00
- Set to `false` to disable websockets with Socket.IO.
2016-03-22 18:11:11 +01:00
## Usage
2016-04-04 21:51:00 +02:00
By default Strapi binds Socket.IO and your common websockets features are available using the `io` object.
2016-03-22 18:11:11 +01:00
2016-04-21 11:39:18 +02:00
Server-side example:
2016-03-22 18:11:11 +01:00
```js
io.on('connection', function (socket) {
socket.emit('news', {
hello: 'world'
});
socket.on('my other event', function (data) {
console.log(data);
});
});
```
2016-04-21 11:39:18 +02:00
Browser-side example:
```html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:1337');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {
my: 'data'
});
});
</script>
```