Visitar URL original
Add example demonstrating a basic WebSocket protocol with message types · Issue #217 · coder/websocket · GitHub
Skip to content

Add example demonstrating a basic WebSocket protocol with message types #217

Description

@kabaluyot

In HTML5 websockt or socket.io, you can consume messages just with the following lines of code:

var io = require('socket.io-client');
var socket = io('http://localhost:6001');

socket.on('connect', function () {
	console.log('connected');
});

socket.on('StockTicker', function (data) {
	console.log(data);
});

socket.on('StockBidAsk', function (data) {
	console.log(data);
});

socket.on('Stock', function (data) {
	console.log(data);
});

How to achieve the example above in this library?

Activity

  1. nhooyr commented on Mar 27, 2020

    @nhooyr
    Contributor

    This library provides no way to route messages, you'd have to do that yourself. You could via a map, a switch statement etc.

    See the examples for how to read and write messages.

  2. kabaluyot commented on Mar 27, 2020

    @kabaluyot
    Author

    the chat example sir? If you may sir, can you provide a basic example for websocket client with "StockTicker" and "Stock" as channels (rooms). The examples are quite confusing esp to beginners and coming from different language like me :)

  3. nhooyr commented on Mar 27, 2020

    @nhooyr
    Contributor

    I understand, let's add a simple example showing how to route different messages.

  4. reopened this on Mar 27, 2020
  5. kabaluyot commented on Mar 27, 2020

    @kabaluyot
    Author

    Thank you sir. Maybe you can send just a guide or sample here and I will make a PR

  6. modified the milestones: v1.8.5, v1.9.0 on Apr 13, 2020
  7. changed the title [-]Example on how to consume socket connection type messages (like in HTML5 websocket or socket.io)[/-] [+]Add example demonstrating a basic WebSocket protocol[/+] on Apr 14, 2020
  8. changed the title [-]Add example demonstrating a basic WebSocket protocol[/-] [+]Add example demonstrating a basic WebSocket protocol with different requests[/+] on Apr 14, 2020
  9. changed the title [-]Add example demonstrating a basic WebSocket protocol with different requests[/-] [+]Add example demonstrating a basic WebSocket protocol with message types[/+] on Apr 14, 2020
  10. pymq commented on Apr 14, 2020

    @pymq

    Hey @kabaluyot

    We had the same problem, so we ended up with writing simple socket.io alternative isp-etp-go on top of nhooyr/websocket, maybe you'd be interested.

  11. kabaluyot commented on Apr 15, 2020

    @kabaluyot
    Author

    Thank you @pymq, will definitely check it out. For the moment, I was forced to do the client implementation using Typescript :/

  12. dafinley commented on May 17, 2020

    @dafinley

    So I created this fork with the chat example upgraded for specific chat rooms.
    In total, it has 4 endpoints

    /subscribe - Subscribe to all messages sent to the websocket server
    /subscribe/ - Subscribe to all messages from a specific room (i.e. /subscribe/cool)

    /publish - Publish to every single listener regardless of whether they are in a room or not
    /publish/ - Publish to your room and any global subscribers (i.e publish/cool)

    https://github.com/dafinley/websocket/tree/master/examples/chat

  13. dafinley commented on May 18, 2020

    @dafinley
  14. kabaluyot commented on May 18, 2020

    @kabaluyot
    Author

    Well appreciated @dafinley . Thank you

  15. modified the milestones: v1.9.0, v1.8.7 on May 18, 2020
  16. nhooyr commented on May 19, 2020

    @nhooyr
    Contributor

    With inspiration from @dafinley I think the best move here is to extend the chat example to add structure like timestamps to each message and add two more message types to indicate when a client connects/leaves.

  17. self-assigned this
    on May 19, 2020
  18. dafinley commented on May 19, 2020

    @dafinley

    @nhooyr smooth websocket lib man. This was my intro into go. If anybody lands here I did a walkthru video on using this chat example https://www.youtube.com/watch?v=Jhnv5tA6Waw

  19. deleted a comment from koloman4 on May 20, 2020
  20. modified the milestones: v1.8.7, v1.9.0 on Jan 9, 2021
  21. nhooyr commented on Aug 6, 2021

    @nhooyr
    Contributor

    See #309 and #301 for more problems the new example should fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions