Coder Social home page Coder Social logo

Comments (6)

thoov avatar thoov commented on June 14, 2024

@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.

knownasilya avatar knownasilya commented on June 14, 2024

Thanks @thoov!

from ember-websockets.

yratanov avatar yratanov commented on June 14, 2024

@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.

yratanov avatar yratanov commented on June 14, 2024

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.

thoov avatar thoov commented on June 14, 2024

@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.

thoov avatar thoov commented on June 14, 2024

I am closing this issue. I have updated the readme for both of these examples

from ember-websockets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.