Comments (6)
@knownasilya Thanks for the feedback! Yes that is possible and I will add an example of how this can been done. But until then here is a quick snippet:
First add the routes to your router.js like so:
Router.map(function() {
this.resource('chatrooms', function() {
this.route('foo');
this.route('bar');
});
});
Then setup both of the routes (foo and bar in this example):
// foo.js
import socketMixin from 'ember-websockets/mixins/sockets';
export default Ember.Route.extend(socketMixin, {
socketURL: 'ws://localhost:8080/room1'
});
// bar.js
import socketMixin from 'ember-websockets/mixins/sockets';
export default Ember.Route.extend(socketMixin, {
socketURL: 'ws://localhost:8080/room2'
});
Then setup controllers for both foo and bar routes:
// foo.js
export default Ember.Controller.extend({
actions: {
onmessage: function(socketEvent) {
console.log('On message has been called for foo.js');
}
}
});
// bar.js
export default Ember.Controller.extend({
actions: {
onmessage: function(socketEvent) {
console.log('On message has been called for bar.js');
}
}
});
At this point you will be good to go. When ever you visit the chatroom/foo or chatroom/bar routes you will be "communicating" with the socket on different "chatrooms". I will be adding better/more examples in the dummy folder within the next week or so.
Let me know if this helps or if you need anything more explained as I would be more than happy to help.
from ember-websockets.
Thanks @thoov!
from ember-websockets.
@thoov I believe I have related question. I have several chat rooms with routes e.g. room/1
, room/2
, etc.
Currently I have the following code:
updateSocketURL: ->
@set('socketURL', "wss://localhost/chat/#{@modelFor('room').get('id')}")
setupController: (controller, model)->
@updateSocketURL()
@_super(controller, model)
So the socket url is updated every time route changes, but ember-websocket uses previously created socket anyway. How can I close/open socket when route changes? Am I doing something wrong?
from ember-websockets.
ok, I ended up with this solution:
updateSocketURL: (model)->
modelId = model.get('id')
@get('socketConfigurations').push
key: "chat-#{modelId}"
socketURL:"wss://localhost/chat/#{modelId}"
setupController: (controller, model)->
@updateSocketURL(model)
@_super(controller, model)
Not so pretty, but working...
from ember-websockets.
@yratanov Thanks for the question! Your first way that you listed should work but when I tested this locally I discovered a bug. The issue that you pointed out is that I was relying on the deactivate method to be called in order to indicate when I should close the socket. However if you have a route with a dynamic segment and you transition from say room/1 to room/123 then deactivate never gets called and thus the original socket never closes. I have a fix for this and I will push an update to npm right now. If you could download v0.6.0 and try it out and let me know if it is working for you I would greatly appreciate it.
from ember-websockets.
I am closing this issue. I have updated the readme for both of these examples
from ember-websockets.
Related Issues (20)
- An in-range update of loader.js is breaking the build 🚨 HOT 3
- An in-range update of broccoli-merge-trees is breaking the build 🚨 HOT 2
- 7.0.1 & websockets - unable to find socket.io.min.js HOT 4
- Implement sub protocol STOMP HOT 5
- not working as a dependency of an addon HOT 2
- idea: error if accidentally used in fastboot with a good error message
- NA
- New release for 2.6/2.17 HOT 3
- Unable to find socket.io.min.js HOT 3
- Make socket.io optional
- Force new socket.io connection
- Release new version HOT 2
- Browser Support (IE 11) HOT 1
- Losing websocket callback handlers when calling socketFor
- Cannot read property 'commit' of null HOT 4
- Getting the error io is not defined HOT 1
- closeSocketFor does not destroy the web socket and causes memory leak HOT 3
- ReferenceError: io is not defined HOT 5
- Plans on upgrading to Ember Octane? HOT 2
- Error updating to 10.2.1 socket.on('connect') event not firing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-websockets.